Overriding Object.ToString() | | |
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 | | | | re: Overriding Object.ToString()
Joe,
The "Public Overrides" function should work.
[color=blue]
> Public Overrides Function ToString() as String,[/color]
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" <joe_dh_@hotmail.com> wrote in message
news:1066311908.397578@Virginia.BMTS.Com...[color=blue]
> 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[/color]
to[color=blue]
> list the item in the ComboBox (I'm not interested in DataBinding).
>
> Thanks,
>
> Dave Hagedorn
>
>[/color] | | | | re: Overriding Object.ToString()
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]" <Jay_Harlow@email.msn.com> wrote in message
news:e3stWK$kDHA.2444@TK2MSFTNGP09.phx.gbl...[color=blue]
> Joe,
> The "Public Overrides" function should work.
>[color=green]
> > Public Overrides Function ToString() as String,[/color]
>
> 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" <joe_dh_@hotmail.com> wrote in message
> news:1066311908.397578@Virginia.BMTS.Com...[color=green]
> > Stupid question, but I'm really stuck
> >
> > I have a class that overrides ToString(). When this class is cast back[/color][/color]
to[color=blue][color=green]
> > 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[/color][/color]
invoked[color=blue]
> to[color=green]
> > list the item in the ComboBox (I'm not interested in DataBinding).
> >
> > Thanks,
> >
> > Dave Hagedorn
> >
> >[/color]
>
>[/color] | | | | re: Overriding Object.ToString()
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.
[color=blue]
> Public Class cStringMapping
> Inherits Object '// different[/color]
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.
[color=blue]
> Public Sub New(ByVal Text As String, ByVal Tag As Object)[/color]
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" <joe_dh_@hotmail.com> wrote in message
news:1066315806.983135@Virginia.BMTS.Com...[color=blue]
> 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[/color]
class[color=blue]
> 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]" <Jay_Harlow@email.msn.com> wrote in[/color]
message[color=blue]
> news:e3stWK$kDHA.2444@TK2MSFTNGP09.phx.gbl...[color=green]
> > Joe,
> > The "Public Overrides" function should work.
> >[color=darkred]
> > > Public Overrides Function ToString() as String,[/color]
> >
> > 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" <joe_dh_@hotmail.com> wrote in message
> > news:1066311908.397578@Virginia.BMTS.Com...[color=darkred]
> > > Stupid question, but I'm really stuck
> > >
> > > I have a class that overrides ToString(). When this class is cast[/color][/color][/color]
back[color=blue]
> to[color=green][color=darkred]
> > > 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[/color][/color][/color]
an[color=blue][color=green][color=darkred]
> > > object of this class to a ComboBox and have its ToString() method[/color][/color]
> invoked[color=green]
> > to[color=darkred]
> > > list the item in the ComboBox (I'm not interested in DataBinding).
> > >
> > > Thanks,
> > >
> > > Dave Hagedorn
> > >
> > >[/color]
> >
> >[/color]
>
>[/color] | | | | re: Overriding Object.ToString()
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 | | | | re: Overriding Object.ToString()
Dave,
You can email me the project, I'll take a look.
Which OS are you on? (which should not matter!)
[color=blue]
> .NET 1.1 (1.1.4332, to be exact)[/color]
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" <joe_dh_@hotmail.com> wrote in message
news:1066322750.611161@Virginia.BMTS.Com...[color=blue]
> 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[/color]
I'll[color=blue]
> switch to databinding.
>
> Thanks again,
>
> Dave Hagedorn
>
>[/color] | | | | re: Overriding Object.ToString()
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" <joe_dh_@hotmail.com> wrote in message
news:1066322750.611161@Virginia.BMTS.Com...[color=blue]
> 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[/color]
I'll[color=blue]
> switch to databinding.
>
> Thanks again,
>
> Dave Hagedorn
>
>[/color] |  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|