473,671 Members | 2,570 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 4668
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
3768
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
3328
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
1106
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
4666
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
3717
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
3737
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
105179
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
3023
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
8828
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...
1
8605
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7446
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
6238
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
5704
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.