Results 1 to 10 of 10

Thread: [RESOLVED] [2005] Overide ToString of a Proeprty

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Resolved [RESOLVED] [2005] Overide ToString of a Proeprty

    Is it possible to override the ToString function of a property? What I have is a Property of an integer type, and throughout my code I need to convert the integer to Hex and if that hex value is only 1 digit add a zero at the begining. I would like to be able to just call ToString of the property instead of creating a function that I have to call everytime I need the conversion.

    So say I have a value of 10
    I convert that to Hex: A
    I then check if A is 1 digit: It is.
    I add 0 to the begining to make 0A.

    I would like to just be able to say Property1.ToString and have the code to do this in the overriden ToString Function.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  2. #2
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: [2005] Overide ToString of a Proeprty

    Personally, I can think of very few times that I would
    want to override the tostring method, why not try
    something like this:
    If me.Textbox1.Text.tostring.length = 1 Then
    strHex = Me.TextBox1.Text.PadLeft(2, "0").ToString
    End If
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Overide ToString of a Proeprty

    I have something similar to that now, but with what I an doing now I have to do it rather often. I am communicating with the serial port, which requires certain things in two digit hex. So it would be nice and save a bit of time if I could just override tostring, and I would be able to avoid having to create a shared class or a module with a function in it.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  4. #4
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Overide ToString of a Proeprty

    What is the difference between calling a function and using a method (or property as you called it)?

    VB Code:
    1. Dim MyInt As Integer
    2. MyInt.ToString("blah") 'assuming you could override it
    3. ToHex(MyInt) 'call a function that would do the same thing

    I don't see how you would gain anything by using an overloaded function. I am probably missing something though.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Overide ToString of a Proeprty

    No... I want to override the ToString of a property, not of a value type.

    I've created an object that uses interfaces. So I am working with quite a few classes (about 10). And in most of those clases I have some properties that I get the integer values from the user, and then they need to be converted to hex. Instead of having a Function ToHex(MyInt) in each class, I would like to be able to call MyProperty.ToString.

    Example
    VB Code:
    1. Public Class Class1
    2.   Private _Speed as Integer
    3.   Public Property Speed as Integer
    4.     Get
    5.        return _Speed
    6.     End Get
    7.     Set(ByVal value as Integer)
    8.        _Speed = value
    9.     End Set
    10.   End Property
    11.  
    12. End Class
    13.  
    14. Public Class2
    15.   Private _class1 As New Class1
    16.   'user has set it with 10
    17.   'So I need to convert that to Hex
    18.  
    19.   Dim HexValue As String = Hex(_class1 .Speed) 'Will give me A
    20.  
    21.   'So then need to check if it is 2 digits and add a digit if needed in this case I do need to
    22.    If HexValue.Length = 1
    23.       HexValue = HexValue.Insert(0, "0")
    24.    End If
    25.  
    26.    'What I would like to do if possible is override the Proeprty's ToString to do this
    27.    HexValue = _class1.Speed.ToString ' HexValue is now "0A"
    28.  
    29.   Public Sub ChangeDirection()
    30.      Dim cmd_PanTiltStop as String = "8" & m_CameraId & "010601" & HexValue  & "0303FF"
    31.   End
    32.  
    33. End Class
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Overide ToString of a Proeprty

    Writing it all out like that, I believe I would be just waisting time in doing it like that. I should probably just create readonly string property that does the conversion.

    So I think I will go that route. I think I may have been over anylizing it a bit. I gues that's what happens when you work through the weekend for the third week in a row. But I would like to know if it is possible, just out of curiousity now.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Overide ToString of a Proeprty

    As far as I know, you can not override that.

    If Int32 was a class that you could inherit from, you could make your own class and override the ToString method, but it is a structure, not a class.

    What you COULD do, is write a tiny formatting class, that implements IFormatProvider (and possibly ICustomFormatter) and when you call the Int32.ToString method, use the overloaded version that takes an IFormatProvider class, and pass an instance of your formatting class, that formats integers into hex.

  8. #8
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [RESOLVED] [2005] Overide ToString of a Proeprty

    What you're doing is a bit complicated. To override the ToString method of a class, it's very simple. Just type in "Public Override" in the IDE and it'll automatically show you all the methods you can override (or it does that in C#, I believe VB is the same but I have no way to check right now).

    Now since you want to override the ToString method on a property, then you're going to have to return a custom class from that property.

    So if your property is returning an Int, you're going to have to create a class that Inherits from Int and Override its ToString() method. Then, when you use that in your property, it'll work as you want it.

    EDIT: Actually, you can't Inherit an Int. Forgot it was a struct and not a class. Heh
    Quote Originally Posted by bgard68
    Personally, I can think of very few times that I would
    want to override the tostring method
    Override base methods is very useful. Whenever you create a custom class (one that doesn't inherit a class that has ToString working properly), it only returns the name of the class. Kind of useless.

    Say you make a class called Person. If you called ToString(), you'd probably get 'MyClasses.Person' from it.
    Last edited by Kasracer; Mar 27th, 2006 at 02:31 PM.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [RESOLVED] [2005] Overide ToString of a Proeprty

    Thanks Kleinma for the idea of a workaround, and thanks eye and bgard for you inputs. Once I started typing it out in pseudo code I realized, it wasn't likely to be possible, and it would be just as easy to create a readonly Property that did it. Sometimes I just need to talk it out with someone to realize the error.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] [2005] Overide ToString of a Proeprty

    Quote Originally Posted by mpdeglau
    Thanks Kleinma for the idea of a workaround, and thanks eye and bgard for you inputs. Once I started typing it out in pseudo code I realized, it wasn't likely to be possible, and it would be just as easy to create a readonly Property that did it. Sometimes I just need to talk it out with someone to realize the error.
    I do that often with values that I want spit out as strings always looking a certain way, so that I don't always need to provide a format for them when using the classes in code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width