Can't override CultureInfo.ToString

P

Phil Jollans

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
 
Y

Yuichiro Ochifuji

Hi,

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

Phil Jollans

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
 
J

Jay B. Harlow

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
 
P

Phil Jollans

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/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=235465
since I still think it is a bug. Now I have two good alternatives so it is
not at all urgent.

Phil
 
G

Guest

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.
 
P

Phil Jollans

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/library/19fc31ss(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top