Overriding Object.ToString()

J

Joe

Stupid question, but I'm really stuck

I have a class that overrides ToString(). When this class is cast back to
Object, and ToString()
called, Object.ToString() is called instead. I've tried everything:

Public Overrides Function ToString() as String,
Overrides Function ToString() as String,
Overrides Overloads ToString() as string,
Shadows ToString() as String,
etc...

Not that it should make any difference, but I want to be able to add an
object of this class to a ComboBox and have its ToString() method invoked to
list the item in the ComboBox (I'm not interested in DataBinding).

Thanks,

Dave Hagedorn
 
J

Jay B. Harlow [MVP - Outlook]

Joe,
The "Public Overrides" function should work.
Public Overrides Function ToString() as String,

Simple demonstration:

Public Class Name

Private Readonly m_name As String

Public Sub New(ByVal name As String)
m_name = name
End Sub

Public Overrides Function ToString() As String
Return m_name
End Function

End Class

Dim joe As New Name("joe")
Dim obj as Object = joe
Debug.WriteLine(joe, "Joe")
Debug.WriteLine(obj, "Object")

Was something else going on that the above did not work?

Hope this helps
Jay
 
J

Joe

Hi,

Thanks for the help. I've done pretty much exactly as in your example,
unless I missed something.

Here's my class:

Public Class cStringMapping

Private pText As String
Private pTag As Object

Public Sub New(ByRef Text As String, ByRef Tag As Object)
Me.pText = Text
Me.pTag = Tag
End Sub

Public Overrides Function ToString() As String
Return "asdljfsadlfa;sfd"
End Function
End Class

Now, if I do:

Dim map As cStringMapping = new cStringMapping("test","ing")
Dim obj As Object = map

Debug.WriteLine(map,"myClass")
Debug.WriteLine(obj,"obj")

I'll get:

myClass: cStringMapping
obj: cStringMapping

The only time the ToString() ever worked properly was when I wrote my class
declaration like:

Public Class cStringMapping
Inherits Object '// different
.....
End Class

For some reason, this worked properly for a while, but after a few
recompiles, has failed again.

Thanks again for your help,

Dave Hagedorn
 
J

Jay B. Harlow [MVP - Outlook]

Dave,
(why does your from say Joe? In other words do you prefer Joe or Dave?)

Which version of .NET are you on?

What I gave and you gave works in both VS.NET 2002 & VS.NET 2003.
Public Class cStringMapping
Inherits Object '// different

The VB.NET compiler does that for you, you don't need to include it.

BTW: This has no baring on your problem, you do not need to use ByRef on
your constructor.
Public Sub New(ByVal Text As String, ByVal Tag As Object)

Strings in VB.NET are now true references, when you pass the string ByVal
you are getting a copy of the reference, there is only one string object on
the heap.

Generally I find it better to use ByVal for parameters unless you actually
need ByRef, you only need ByRef when you need to modify the caller's
variable from the callee (return a value).

Hope this helps
Jay
 
J

Joe

Hi again,

..NET 1.1 (1.1.4332, to be exact)

I could send you an example VS solution if you like, otherwise I think I'll
switch to databinding.

Thanks again,

Dave Hagedorn
 
J

Jay B. Harlow [MVP - Outlook]

Dave,
You can email me the project, I'll take a look.

Which OS are you on? (which should not matter!)
.NET 1.1 (1.1.4332, to be exact)
Is that a typo? 1.1.4332 or 1.1.4322? I show 1.1.4322 under help about for
VS.NET 2003.

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

Joe,
I tried the sample you sent me, I've ran it about 5 or 6 times I get
"SomeText" in both text boxes as expected.

Not sure why it wouldn't work for you.

Did you previously have any beta versions of .NET on your machine?

Did you have problems installing .NET or VS.NET?

Have you tried the Repair option for VS.NET?

Have you uninstalled & reinstalled VS.NET & .NET Framework?

Hope this helps
Jay
 

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

Top