Home All Groups Group Topic Archive Search About

Can't override CultureInfo.ToString

Author
29 Oct 2006 8:29 PM
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

Author
30 Oct 2006 12:36 PM
Yuichiro Ochifuji
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.(^^;
Author
30 Oct 2006 8:53 PM
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


Show quoteHide quote
"Yuichiro Ochifuji" <ochif***@japan.interq.or.jp> wrote in message
news:%23VUgCAC$GHA.1196@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.(^^;
Author
2 Nov 2006 2:08 AM
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



--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Phil Jollans" <nospam@jollans.com> wrote in message
news:ei32ug$2ot$03$1@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
>
>
>
Author
4 Nov 2006 11:47 AM
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
Author
4 Nov 2006 7:39 PM
Jay B. Harlow
> 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


Show quote Hide quote
"Phil Jollans" <nospam@jollans.com> wrote in message
news:eihujs$noj$01$1@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/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
>
Author
4 Nov 2006 5:41 PM
Peter Ritchie [C# MVP]
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#


Show quoteHide quote
"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
>
>
>
>
Author
4 Nov 2006 7:40 PM
Jay B. Harlow
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


Show quoteHide quote
"Peter Ritchie [C# MVP]" <PRSoCo@newsgroups.nospam> wrote in message
news:135A81A5-C149-492D-982D-5103A89BC93B@microsoft.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
>>
>>
>>
>>
Author
5 Nov 2006 7:19 PM
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