[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.
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
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.
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:
Dim MyInt As Integer
MyInt.ToString("blah") 'assuming you could override it
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. :)
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:
Public Class Class1
Private _Speed as Integer
Public Property Speed as Integer
Get
return _Speed
End Get
Set(ByVal value as Integer)
_Speed = value
End Set
End Property
End Class
Public Class2
Private _class1 As New Class1
'user has set it with 10
'So I need to convert that to Hex
Dim HexValue As String = Hex(_class1 .Speed) 'Will give me A
'So then need to check if it is 2 digits and add a digit if needed in this case I do need to
If HexValue.Length = 1
HexValue = HexValue.Insert(0, "0")
End If
'What I would like to do if possible is override the Proeprty's ToString to do this
HexValue = _class1.Speed.ToString ' HexValue is now "0A"
Public Sub ChangeDirection()
Dim cmd_PanTiltStop as String = "8" & m_CameraId & "010601" & HexValue & "0303FF"
End
End Class
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.
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.
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.
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.
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.