ToString()

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a property where I want to associate tostring with
it. So that this would be valid:

-----
Dim x as string

x = myobject.myproperty.tostring
-----

I know I have to overide tostring() but where do I do
that to get my desired result?

Thanks!
 
I have a property where I want to associate tostring with
it. So that this would be valid:

-----
Dim x as string

x = myobject.myproperty.tostring
-----

I know I have to overide tostring() but where do I do
that to get my desired result?

Thanks!

Normally, the override would appear in the instantiated class. In your
example, the class that was used to create myobject.
 
I have a property where I want to associate tostring with
it. So that this would be valid:

'ToString' must be implemented in the type of the property or one of its
base classes. Example:

\\\
Public Class Person
Private m_Age As Integer
Private m_Name As String

Public Property Age() As Integer
Get
Return m_Age
End Get
Set(ByVal Value As Integer)
m_Age = Value
End Set
End Property

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property

Public Overrides Function ToString() As String
Return Name & " (" & CStr(Age) & ")"
End Function
End Class
..
..
..
Dim p As New Person()
p.Age = 44
p.Name = "Pink Panther"
MsgBox(p.ToString()) ' "Pink Panther (44)".
///
 
So would it look like this:

Public MyClass
Private _myFirstProperty As Integer
Private _mySecondProperty As Integer

Public ReadOnly Property MyFirstProperty() As Integer
Get
Return 100
End Get
End Property

Public ReadOnly Property MySecondProperty() As Integer
Get
Return 200
End Get
End Property


Public Overrides FunctionToString() As String
Return "Two Hundred"
End Function
End Class

....but using this I can only see tostring that I just
overrode like this:

Myobj.Tostring

I want to be able to see it like this:

Myobj.MyFirstProperty.ToString
Myobj.MySecondProperty.ToString
-----Original Message-----

I have a property where I want to associate tostring with
it. So that this would be valid:

-----
Dim x as string

x = myobject.myproperty.tostring
-----

I know I have to overide tostring() but where do I do
that to get my desired result?

Thanks!

Normally, the override would appear in the instantiated class. In your
example, the class that was used to create myobject.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.


.
 
Dim p As New Person()
p.Age = 44
p.Name = "Pink Panther"
MsgBox(p.ToString()) ' "Pink Panther (44)".

How will my class construct look if I want access it like
this?

p.Age.Tostring '"Pink Panther"
 
Public ReadOnly Property MyFirstProperty() As Integer
[...]
Public ReadOnly Property MySecondProperty() As Integer
[...]
I want to be able to see it like this:

Myobj.MyFirstProperty.ToString
Myobj.MySecondProperty.ToString

That will work without any additional code. The first line will call the
'ToString' method of the 'Integer' returned by 'MyFirstProperty' (the return
value of 'ToString' will be "100"), and the 2nd line will do the same for
the 2nd property (the return value will be "200").
 
How will my class construct look if I want access it like
this?

p.Age.Tostring '"Pink Panther"

'Age' is an 'Integer', so calling 'ToString' on it will return a string
representation of the integer's value. Use 'p.Name' directly to get the
name of the person (which is already a string, why you don't need to call
'ToString').
 
Yes that's true as is but I want to override
the "tostring" method so that it returns like this:

Myobj.MyFirstProperty.Tostring 'Returns "One-Hundred"
Myobj.MySecondProperty.ToString 'Returns "Two-Hundred"

-----Original Message-----
Public ReadOnly Property MyFirstProperty() As Integer
[...]
Public ReadOnly Property MySecondProperty() As Integer
[...]
I want to be able to see it like this:

Myobj.MyFirstProperty.ToString
Myobj.MySecondProperty.ToString

That will work without any additional code. The first line will call the
'ToString' method of the 'Integer' returned
by 'MyFirstProperty' (the return
 
The problem that you have here is that ToString() is a method of a type. A
property is an instantiation of a type (in your examples, an Integer type).

If you want your properties to have their own "ToString()" methods you must
subclass the type in question, override that type's ToString(), and declare
the properties to be of the new type. The problem there is that the integral
types (System.Integer etc) are sealed - you cannot derive new classes from
them. So, that means you are going to have to create your own new custom
types that do everything you want.

The question I would have though is why? Why do you absolutely positively
need to override ToString(). Why can't you create a shared method on utility
class that renders the values in your properties in the way you want. There
must be another way surely?


--
Pete Wright
Author of ADO.NET Novice to Pro for Apress
www.petewright.org


Yes that's true as is but I want to override
the "tostring" method so that it returns like this:

Myobj.MyFirstProperty.Tostring 'Returns "One-Hundred"
Myobj.MySecondProperty.ToString 'Returns "Two-Hundred"

-----Original Message-----
Public ReadOnly Property MyFirstProperty() As Integer
[...]
Public ReadOnly Property MySecondProperty() As Integer
[...]
I want to be able to see it like this:

Myobj.MyFirstProperty.ToString
Myobj.MySecondProperty.ToString

That will work without any additional code. The first line will call the
'ToString' method of the 'Integer' returned
by 'MyFirstProperty' (the return
value of 'ToString' will be "100"), and the 2nd line will do the same for
the 2nd property (the return value will be "200").

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top