473,468 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

inconsistent toString behavior

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=e...gbl%26rnum%3D4)

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
Nov 20 '05 #1
3 1022
"Nathan R." <na*********@spindriftconsulting.com> schrieb
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=e...gbl%26rnum%3D4)
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

Nov 20 '05 #2
* na*********@spindriftconsulting.com (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=e...gbl%26rnum%3D4)

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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
> 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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Denis S. Otkidach | last post by:
I've noticed that the order of attribute lookup is inconsistent when descriptor is used. property instance takes precedence of instance attributes: >>> class A(object): .... def...
1
by: bk_kl | last post by:
Hi, I think the following behavior of the build in function 'split' is inconsistent. What do you think? >>> "".split() >>> "".split(";")
7
by: Alex Vorobiev | last post by:
hi there, i am using sql server 7. below is the stored procedure that is giving me grief. its purpose it two-fold, depending on how it is called: either to return a pageset (based on page...
9
by: Marina | last post by:
Here is the problem. If 2 different properties on the same (or different) control are bound to the same data column, changing the Text property and calling EndCurrentEdit discards the new value. ...
7
by: Matt Silberstein | last post by:
Here is an odd one. Take a look at http://www.nyccure.com/test/changes.html using Firefox. Now reload the page a few times. It will change. All I have is a ul with some images as list items. ...
0
by: Itai | last post by:
Background: I have four Web Form pages with respective C# code behind files, all in the same project: localhost/vpath1 Page1.aspx Page2.aspx
20
by: Francine.Neary | last post by:
I am learning C, having fun with strings & pointers at the moment! The following program is my solution to an exercise to take an input, strip the first word, and output the rest. It works fine...
9
by: Lie | last post by:
I've noticed some oddly inconsistent behavior with int and float: Python 2.5.1 (r251:54863, Mar 7 2008, 03:39:23) on linux2 -345 works, but Traceback (most recent call last): File...
6
by: Kai-Uwe Bux | last post by:
Juha Nieminen wrote: I think your code violates the One-Definition-Rule. The template function foo() is defined in two significantly different ways in the two files. As far as I know, no...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.