How to Create Methods Similar to ToString

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

In a VB.NET app, I can write

Dim s As String = 3.ToString

This is because 3 is of type Int32, which is a structure, which has a method
ToString.

I would also like to be able to write

Dim s As String = 3.ToPercent

by supplying my own implementation of ToPercent. If I could do this, then I
would be able to do what I ultimately wish to do, and that is write
something like the following:

Dim mc As New MyClass
Dim i As Integer

i = mc.Value
Textbox1.Text = mc.Value.ToPercent

I have tried to make Value of type MyValueClass, for example, which inherits
from Int32, but Int32 is not inheritable, so that is a non-starter. In any
case, the first assignment gives a compile time error because there is no
default property of MyValueClass, and I can't set one unless it takes
parameters (which it doesn't).

Can anyone suggest how this might be done?

TIA

Charles
 
Charles Law said:
In a VB.NET app, I can write

Dim s As String = 3.ToString

This is because 3 is of type Int32, which is a structure, which has a
method ToString.

I would also like to be able to write

Dim s As String = 3.ToPercent

by supplying my own implementation of ToPercent. If I could do this,
then I would be able to do what I ultimately wish to do, and that is
write something like the following:

Dim mc As New MyClass
Dim i As Integer

i = mc.Value
Textbox1.Text = mc.Value.ToPercent

I have tried to make Value of type MyValueClass, for example, which
inherits from Int32, but Int32 is not inheritable, so that is a
non-starter. In any case, the first assignment gives a compile time
error because there is no default property of MyValueClass, and I
can't set one unless it takes parameters (which it doesn't).

Can anyone suggest how this might be done?


As you've noticed, you can not extend a not inheritable class. Don't the
overloaded versions of int32.tostring offer what you need? If not, you must
write a separate function to perform the conversion.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Hi Armin

The attraction of a ToPercent method is that it does exactly what it says on
the tin. If I use ToString then I have to provide the format or formatter
each time I call it. With regard to writing my own function, I am quite
prepared for this, but the problem then is how to wire it in so that it is
called like, behaves like and has the same syntax as ToString.

Is the answer to have a MyValueClass class and make a ToPercent method, and
if so, how do I create a default property? As I say, my real target is to be
able to write something like

Dim mc As New MyClass
Dim i As Integer

i = mc.Value
Textbox1.Text = mc.Value.ToPercent

Thanks

Charles
 
Charles Law said:
Hi Armin

The attraction of a ToPercent method is that it does exactly what it
says on the tin. If I use ToString then I have to provide the format
or formatter each time I call it. With regard to writing my own
function, I am quite prepared for this, but the problem then is how
to wire it in so that it is called like, behaves like and has the
same syntax as ToString.

Is the answer to have a MyValueClass class and make a ToPercent
method, and if so, how do I create a default property? As I say, my
real target is to be able to write something like

Dim mc As New MyClass
Dim i As Integer

i = mc.Value
Textbox1.Text = mc.Value.ToPercent

Thanks


I understand your intention and see the advantage of this method. What about
adding an Integer property to a derived Textbox class? I think this is the
better place, because the readable value of an Integer is not relevant
before it is displayed somewhere, like in a textbox. The setter of the Value
property would convert it to a string and assign it to the Text property.
Later in the code you always have to write "percenttextbox1.value = 3" only.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Charles,
Neither VB.NET 2002 nor VB.NET 2003 support what you are attempting, per se.

You could always:

Public Structure MyValueClass

Public Function ToPercent() As String
End Function

Public Function ToInteger() As Integer
End Function

End Structure
i = mc.Value.ToInteger()
Textbox1.Text = mc.Value.ToPercent()


With VB.NET 2005 (Whidbey) we will be able to override CType, the conversion
operator, so you will be able to do something like (based on the CTP release
of Whidbey):

Public Structure MyValueClass

Public Function ToPercent() As String
End Function

Public Function ToInteger() As Integer
End Function

Public Shared Widening Operator CType(value As MyValueClass) As
Integer
Return value.ToInteger()
End Operator

Public Shared Narrowing Operator CType(value As Integer) As
MyValueClass
Return New MyValueClass(value)
End Operator

End Structure

i = mc.Value
mc.Value = CType(i, MyValueClass)

A Widening CType operator does not require CType, while a Narrowing CType
operator does.

Hope this helps
Jay
 
Hi Armin

I think in creating my example I have omitted some of the key detail. In
using ToString as the example, it suggests that I want to format my
percentage as a string. This is actually just incidental. The real purpose
is to convert a value in one range to another range, which in this case is
the range 0 to 100.

A more representative example would be that I have a value x, such that

-Limit <= x <= +Limit

I need to express x as a percentage of this range, so that if Limit = 1000
and x = 500, then ToPercent evaluates to 75.

I will also have a corresponding method ToAbsolute, which will perform the
reverse conversion, solving for x.

I take your point about the textbox scenario, but I used a textbox only for
illustration.

Charles
 
Hi Jay

Thanks for confirming what I was beginning to suspect.

Using the example that I just used to Armin, I would then have

<code>
Public Structure MyValueClass

Public Function ToPercent() As Double
End Function

Public Function ToAbsolute() As Integer
End Function

End Structure

Dim d As Double
Dim i As Integer

d = mc.Value.ToPercent()
i = mc.Value.ToAbsolute()
</code>

showing that I must explicitly call ToAbsolute rather than just write

i = mc.Value

Charles
 
Charles Law said:
Hi Armin

I think in creating my example I have omitted some of the key detail.
In using ToString as the example, it suggests that I want to format
my percentage as a string. This is actually just incidental. The real
purpose is to convert a value in one range to another range, which in
this case is the range 0 to 100.

A more representative example would be that I have a value x, such
that

-Limit <= x <= +Limit

I need to express x as a percentage of this range, so that if Limit =
1000 and x = 500, then ToPercent evaluates to 75.

I will also have a corresponding method ToAbsolute, which will
perform the reverse conversion, solving for x.

I take your point about the textbox scenario, but I used a textbox
only for illustration.


I'm afraid, I don't have a simple solution for this problem. I would
probably use the class you mentioned (Value property representing x, Limit
property, ToPercent function...). You currently can not ommit explicitly
accessing the Value property, but... see Jay's answer (VB 2006)


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Hi Armin
Class MyValue
Private mValue As Object

Public Property Value() As Object
Get
Return mValue
End Get
Set (ByVal Value As Object)
mValue = Value
End Set
End Property

Public Function ToPercent() As Single
Return CSng(Value) / 100
End Function

Public Function ToString() As String
Return CStr(Value)
End Function

Public Function ToInteger() As Integer
Return CInt(Value)
End Function
End Class


Class MyClass
Private mValue As MyValue

Public Property Value() As MyValue
Get
Return mValue
End Property
Set (ByVal Value As MyValue)
mValue = Value
End Set
End Property
End Class



Above is not exactly what you want, but still, can work :)

Mythran
 
Back
Top