473,806 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4679
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*****@hotmai l.com> wrote in message
news:10******** *******@Virgini a.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;s fd"
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********@ema il.msn.com> wrote in message
news:e3******** ******@TK2MSFTN GP09.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*****@hotmai l.com> wrote in message
news:10******** *******@Virgini a.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*****@hotmai l.com> wrote in message
news:10******** *******@Virgini a.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;s fd"
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********@ema il.msn.com> wrote in message news:e3******** ******@TK2MSFTN GP09.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*****@hotmai l.com> wrote in message
news:10******** *******@Virgini a.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*****@hotmai l.com> wrote in message
news:10******** *******@Virgini a.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*****@hotmai l.com> wrote in message
news:10******** *******@Virgini a.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
3774
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
3333
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 class that I have set up for my ListViewItemSorter. Here is my (simplified) code: // Note: there are accessor properties, constructors, etc // that I have omitted here for clarity
2
1109
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 have tried overriding it with the following. Public Overrides Function toString() As String Dim result As System.Text.StringBuilder Dim item As Box For Each item In Me.List result.Append(item.BoxType.ToString) Next End Function
4
4681
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 do it at the control level. Thanks, Ron
4
3724
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
3751
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 == and != opperators. Am I missing something or when would one want to have different implementations for Equals and ==? --Ken
10
105210
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
11
441
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 that?
3
3036
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 this. I have an object that has a function. This function needs to call itself in a setTimeout call. Using the "this" keyword from within the callback function will obviously not work, seeing as it would reference the new function rather than the...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.