inconsistent toString behavior

N

Nathan R.

Hi,

I'm seeing something that seems extremely strange to me. All I'm
doing is overriding the toString method in one of my classes, so that
instances will display the way I'd like in Combo boxes and the like.
But instead of calling the overridden method of the subclass, it looks
like Object.toString is being called instead -- toString returns
"MyProjectName.MyClassName".

So that's strange and mysterious, but that's not the end of it. I
found a post on this group (URL
http://groups.google.com/groups?hl=...elVmkOjDHA.1688%40TK2MSFTNGP12.phx.gbl&rnum=4)

with a similar problem, and no real resolution. Using this, I reduced
the problem code to the following:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim joe As New Name("joe")
Dim obj As Object = joe
Debug.WriteLine(joe, "Joe")
Debug.WriteLine(obj, "Object")
End Sub
End Class
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

The really strange bit? This works in a new project, but not in the
old. In the old, it gives the old behavior, calling Object.toString
instead. I'm running VB.NET 2002, but the above-referenced post
suggests VB.NET 2003 is equally susceptible to whatever this is.

Any ideas?

Thanks much!

Nathan
 
A

Armin Zingler

Nathan R. said:
Hi,

I'm seeing something that seems extremely strange to me. All I'm
doing is overriding the toString method in one of my classes, so
that instances will display the way I'd like in Combo boxes and the
like. But instead of calling the overridden method of the subclass,
it looks like Object.toString is being called instead -- toString
returns "MyProjectName.MyClassName".

So that's strange and mysterious, but that's not the end of it. I
found a post on this group (URL
http://groups.google.com/groups?hl=...elVmkOjDHA.1688%40TK2MSFTNGP12.phx.gbl&rnum=4)

with a similar problem, and no real resolution. Using this, I
reduced the problem code to the following:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles MyBase.Load

Dim joe As New Name("joe")
Dim obj As Object = joe
Debug.WriteLine(joe, "Joe")
Debug.WriteLine(obj, "Object")
End Sub
End Class
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

The really strange bit? This works in a new project, but not in
the old. In the old, it gives the old behavior, calling
Object.toString instead. I'm running VB.NET 2002, but the
above-referenced post suggests VB.NET 2003 is equally susceptible to
whatever this is.


I don't understand the problem. I get "Joe" in all cases, also when the
object is added to a combobox, it displays "Joe". This happens in version
2002 and 2003. It's all how I expect it.


--
Armin

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

Herfried K. Wagner [MVP]

* (e-mail address removed) (Nathan R.) scripsit:
I'm seeing something that seems extremely strange to me. All I'm
doing is overriding the toString method in one of my classes, so that
instances will display the way I'd like in Combo boxes and the like.
But instead of calling the overridden method of the subclass, it looks
like Object.toString is being called instead -- toString returns
"MyProjectName.MyClassName".

So that's strange and mysterious, but that's not the end of it. I
found a post on this group (URL
http://groups.google.com/groups?hl=...elVmkOjDHA.1688%40TK2MSFTNGP12.phx.gbl&rnum=4)

with a similar problem, and no real resolution. Using this, I reduced
the problem code to the following:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim joe As New Name("joe")
Dim obj As Object = joe
Debug.WriteLine(joe, "Joe")
Debug.WriteLine(obj, "Object")
End Sub
End Class
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

The really strange bit? This works in a new project, but not in the
old. In the old, it gives the old behavior, calling Object.toString
instead. I'm running VB.NET 2002, but the above-referenced post
suggests VB.NET 2003 is equally susceptible to whatever this is.

Output in VS.NET 2003:

Joe: joe
Object: joe
 
N

Nathan Roberts

I don't understand the problem. I get "Joe" in all cases, also when
the
object is added to a combobox, it displays "Joe". This happens in version
2002 and 2003. It's all how I expect it.

Yes, I'm afraid I didn't expect others to be able to reproduce the
problem, at
least not from the code alone... I think it must be something within
the
project -- either something's become corrupted within the project (??)
or
there's a bad setting somewhere within the project, or some other
project-
specific issue. Because even on my machine, the exact same code behaves
differently in one project than in another.

Thanks for your reply.

Nathan
 

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