473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Overriding Object.ToString()

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
Nov 20 '05 #1
6 4649
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

"Joe" <jo*****@hotmail.com> wrote in message
news:10***************@Virginia.BMTS.Com... 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

Nov 20 '05 #2
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
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
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

"Joe" <jo*****@hotmail.com> wrote in message
news:10***************@Virginia.BMTS.Com...
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


Nov 20 '05 #3
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
"Joe" <jo*****@hotmail.com> wrote in message
news:10***************@Virginia.BMTS.Com... 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
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:e3**************@TK2MSFTNGP09.phx.gbl...
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

"Joe" <jo*****@hotmail.com> wrote in message
news:10***************@Virginia.BMTS.Com...
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



Nov 20 '05 #4
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
Nov 20 '05 #5
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

"Joe" <jo*****@hotmail.com> wrote in message
news:10***************@Virginia.BMTS.Com... 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

Nov 20 '05 #6
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
"Joe" <jo*****@hotmail.com> wrote in message
news:10***************@Virginia.BMTS.Com...
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

Nov 20 '05 #7

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

Similar topics

2
by: Ovid | last post by:
Hi, I'm trying to determine the cleanest way to override class data in a subclass. class Universe { public String name; private static double PI = 3.1415; Universe(String name) {
3
by: pagates | last post by:
Hello Gurus, I have a listview, and I only want to add unique items. The problem is, my code is blowing by my "Contains" statement, adding the item, and then hitting my Compare code in the...
2
by: ECathell | last post by:
I am having issues with my collection now that i have it mostly together. I am binding it to a listbox, but instead of the box information I want it to show, it is simply showing the object type. I...
4
by: RSH | last post by:
How do I go about overriding a Control's OnPaint Method? I would like to prevent a control's color from changing when it is disabled. I have overridden the Form's OnPaint Method but I need to...
4
by: RSH | last post by:
I tried an implementation of overriding a ComboBox control. I am simply trying to avoid it repainting, but I can't seem to get it to work. What am I doing wrong? Please help. Thanks, Ron
8
by: Kenneth Baltrinic | last post by:
When one overrides the Equals() method of an object, one is supposed to override GetHashCode() as well and this makes good sense. But I have seen lots of people who do this and do not override the...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
11
by: etam | last post by:
Hi, why I can not override it? This line: virtual String^ ToString() override = Object::ToString; produces this error: cannot override base class method 'System::Object::ToString'. Why is...
3
Atli
by: Atli | last post by:
Hi everybody. This is not so much a problem, since I have already managed to find a solution, but my solution requires the use of the eval() function, which I just hate to use. The problem is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.