By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,534 Members | 943 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,534 IT Pros & Developers. It's quick & easy.

Can't override CultureInfo.ToString

P: n/a
Hi,

I am having difficulty overriding the ToString() method of CultureInfo using
Visual Studio 2005.

Exactly the same code works fine with Visual Studio .NET 2003.

What I am doing is adding objects which are derived from CultureInfo to a
ListBox. I want the language name to be displayed in the native language, so
I override the ToString() method to access the CultureInfo.NativeName
property.

I can reproduce the problem with a simple example.

Create a windows forms project in VB.NET and add a single list box to the
form.
Paste the following code into the file Form1.vb (replacing the existing
code).
Imports System.Globalization

Public Class Form1

Private Class CultureListInfo
Inherits CultureInfo

Public Sub New ( Byval Name as String )
MyBase.New ( Name )
End Sub

Public Overrides Function ToString() as String
return NativeName
End Function

End Class

Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

ListBox1.Items.Add ( new CultureListInfo ( "de" ) )
ListBox1.Items.Add ( new CultureListInfo ( "es" ) )
ListBox1.Items.Add ( new CultureListInfo ( "fr" ) )

End Sub

End Class
My understanding is that the ListBox will use the ToString() method to
generate the text shown in the list box. However, the ToString() method is
never entered.

I have almost identical code in C# which works fine, and the above code
worked with VS 2003.

What is wrong?

Phil

Oct 29 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi,

You may override DisplayName property to change the item in ListBox.
The specification might have been changed.
--
Yuichiro Ochifuji
JAPAN
I am not good at English.(^^;
Oct 30 '06 #2

P: n/a
Thanks a lot. I have now used the code

Public Overrides ReadOnly Property DisplayName() As String
Get
Return MyBase.NativeName
End Get
End Property

instead of

Public Overrides Function ToString() As String
Return MyBase.NativeName()
End Function

and this version works.

I don't think that they have changed the specification.

MICROSOFT ARE YOU LISTENING?
I think they have broken it.

Best regards
Phil
"Yuichiro Ochifuji" <oc******@japan.interq.or.jpwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi,

You may override DisplayName property to change the item in ListBox.
The specification might have been changed.

--
Yuichiro Ochifuji
JAPAN
I am not good at English.(^^;

Oct 30 '06 #3

P: n/a
Phil,
What does your C# code look like?
My understanding is that the ListBox will use the ToString() method to
generate the text shown in the list box. However, the ToString() method is
never entered.
That is my understanding also. Although using your code I get the same
results. It feels like a bug. Have you submitted something on
http://connect.microsoft.com/
The following demonstrates that your code is working:
Dim item As CultureInfo
item = New CultureListInfo("de")
Debug.WriteLine(item, "Object.ToString()")
Debug.WriteLine(item.ToString(), "CultureInfo.ToString()")

Prints:
Object.ToString(): Deutsch
CultureInfo.ToString(): Deutsch

Have you considered simply:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
ListBox1.Items.Add(New CultureInfo("de"))
ListBox1.Items.Add(New CultureInfo("es"))
ListBox1.Items.Add(New CultureInfo("fr"))
ListBox1.DisplayMember = "NativeName"
End Sub

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Phil Jollans" <no****@jollans.comwrote in message
news:ei*************@news.t-online.com...
Hi,

I am having difficulty overriding the ToString() method of CultureInfo
using
Visual Studio 2005.

Exactly the same code works fine with Visual Studio .NET 2003.

What I am doing is adding objects which are derived from CultureInfo to a
ListBox. I want the language name to be displayed in the native language,
so
I override the ToString() method to access the CultureInfo.NativeName
property.

I can reproduce the problem with a simple example.

Create a windows forms project in VB.NET and add a single list box to the
form.
Paste the following code into the file Form1.vb (replacing the existing
code).
Imports System.Globalization

Public Class Form1

Private Class CultureListInfo
Inherits CultureInfo

Public Sub New ( Byval Name as String )
MyBase.New ( Name )
End Sub

Public Overrides Function ToString() as String
return NativeName
End Function

End Class

Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

ListBox1.Items.Add ( new CultureListInfo ( "de" ) )
ListBox1.Items.Add ( new CultureListInfo ( "es" ) )
ListBox1.Items.Add ( new CultureListInfo ( "fr" ) )

End Sub

End Class
My understanding is that the ListBox will use the ToString() method to
generate the text shown in the list box. However, the ToString() method is
never entered.

I have almost identical code in C# which works fine, and the above code
worked with VS 2003.

What is wrong?

Phil
Nov 2 '06 #4

P: n/a
Hi Jay,

The C# class is

private class CultureListInfo : CultureInfo
{
public CultureListInfo ( string Name ) : base ( Name ) {}
public override String ToString()
{
return NativeName ;
}
}

I havn't created the same example project.

The suggestion with the DisplayMember property is clearly the easiest
method. I was not familiar with this property. Originally, I started using
the nested class with Visual Studio 2002. Did the DisplayMember exist back
then?

I have posted this issue at
https://connect.microsoft.com/Visual...dbackID=235465
since I still think it is a bug. Now I have two good alternatives so it is
not at all urgent.

Phil
Nov 4 '06 #5

P: n/a
There seems to be two problems here. The first is that ListBox uses
ToString method of the object unless a member name within the object is
specified in the DisplayMember property." isn't the whole story. If ListBox
can convert from your type to String it will do that instead of using
ToString().

The second problem is that CultureInfo defines a converter called
CultureInfoConverter that is used to convert the CultureInfo object to a
String object, completely bypassing the ToString method and using the
DisplayName method.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Phil Jollans" wrote:
Hi,

I am having difficulty overriding the ToString() method of CultureInfo using
Visual Studio 2005.

Exactly the same code works fine with Visual Studio .NET 2003.

What I am doing is adding objects which are derived from CultureInfo to a
ListBox. I want the language name to be displayed in the native language, so
I override the ToString() method to access the CultureInfo.NativeName
property.

I can reproduce the problem with a simple example.

Create a windows forms project in VB.NET and add a single list box to the
form.
Paste the following code into the file Form1.vb (replacing the existing
code).
Imports System.Globalization

Public Class Form1

Private Class CultureListInfo
Inherits CultureInfo

Public Sub New ( Byval Name as String )
MyBase.New ( Name )
End Sub

Public Overrides Function ToString() as String
return NativeName
End Function

End Class

Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

ListBox1.Items.Add ( new CultureListInfo ( "de" ) )
ListBox1.Items.Add ( new CultureListInfo ( "es" ) )
ListBox1.Items.Add ( new CultureListInfo ( "fr" ) )

End Sub

End Class
My understanding is that the ListBox will use the ToString() method to
generate the text shown in the list box. However, the ToString() method is
never entered.

I have almost identical code in C# which works fine, and the above code
worked with VS 2003.

What is wrong?

Phil

Nov 4 '06 #6

P: n/a
Originally, I started using the nested class with Visual Studio 2002. Did
the DisplayMember exist back then?
Yes.

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Phil Jollans" <no****@jollans.comwrote in message
news:ei*************@news.t-online.com...
Hi Jay,

The C# class is

private class CultureListInfo : CultureInfo
{
public CultureListInfo ( string Name ) : base ( Name ) {}
public override String ToString()
{
return NativeName ;
}
}

I havn't created the same example project.

The suggestion with the DisplayMember property is clearly the easiest
method. I was not familiar with this property. Originally, I started using
the nested class with Visual Studio 2002. Did the DisplayMember exist back
then?

I have posted this issue at
https://connect.microsoft.com/Visual...dbackID=235465
since I still think it is a bug. Now I have two good alternatives so it is
not at all urgent.

Phil
Nov 4 '06 #7

P: n/a
Peter,
Thanks for the heads up. I forget about Type Converters...

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Peter Ritchie [C# MVP]" <PR****@newsgroups.nospamwrote in message
news:13**********************************@microsof t.com...
There seems to be two problems here. The first is that ListBox uses
ToString method of the object unless a member name within the object is
specified in the DisplayMember property." isn't the whole story. If
ListBox
can convert from your type to String it will do that instead of using
ToString().

The second problem is that CultureInfo defines a converter called
CultureInfoConverter that is used to convert the CultureInfo object to a
String object, completely bypassing the ToString method and using the
DisplayName method.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Phil Jollans" wrote:
>Hi,

I am having difficulty overriding the ToString() method of CultureInfo
using
Visual Studio 2005.

Exactly the same code works fine with Visual Studio .NET 2003.

What I am doing is adding objects which are derived from CultureInfo to a
ListBox. I want the language name to be displayed in the native language,
so
I override the ToString() method to access the CultureInfo.NativeName
property.

I can reproduce the problem with a simple example.

Create a windows forms project in VB.NET and add a single list box to the
form.
Paste the following code into the file Form1.vb (replacing the existing
code).
Imports System.Globalization

Public Class Form1

Private Class CultureListInfo
Inherits CultureInfo

Public Sub New ( Byval Name as String )
MyBase.New ( Name )
End Sub

Public Overrides Function ToString() as String
return NativeName
End Function

End Class

Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

ListBox1.Items.Add ( new CultureListInfo ( "de" ) )
ListBox1.Items.Add ( new CultureListInfo ( "es" ) )
ListBox1.Items.Add ( new CultureListInfo ( "fr" ) )

End Sub

End Class
My understanding is that the ListBox will use the ToString() method to
generate the text shown in the list box. However, the ToString() method
is
never entered.

I have almost identical code in C# which works fine, and the above code
worked with VS 2003.

What is wrong?

Phil

Nov 4 '06 #8

P: n/a
Hi Peter,

thats very interesting.

However, it doesn't really explain:
- why the behaviour appears to be different with VB and C#
- why the behaviour has changed with VS 2005

More importantly, the Visual Studio Help really explains it differently. In
the topic entitled:
"How to: Add and Remove Items from a Windows Forms ComboBox, ListBox, or
CheckedListBox Control"
http://msdn2.microsoft.com/en-us/lib...ss(VS.80).aspx
it states

"The items displayed are usually strings; however, any object can be used.
The text that is displayed in the control is the value returned by the
object's ToString method."

You can't get much clearer than that. The ToString() method will be called,
period.

By the way, I remember exactly why I started using this method. Originally,
I ported the code from VB6, where items in a ListBox control had an
..ItemData property. This disappeared in .NET, which was a bit of a pain. On
the other hand, I can see that it makes sense that the item list is simply a
list of objects. The recommended way to achieve the same functionality was
to add objects to the list, containing any amount of "itemdata", and to
implement a ToString method on the object.

Phil
Nov 5 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.