Re: Can't override CultureInfo.ToString

Tech-Archive recommends: Speed Up your PC by fixing your registry



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" <nospam@xxxxxxxxxxx> wrote in message news:ei32ug$2ot$03$1@xxxxxxxxxxxxxxxxxxxx
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




.



Relevant Pages

  • Re: No Acessible ToString"
    ... this would definitely work ... indicates that it does know this is the ToString method, ... > Sub WeekHeader(startdate As Date, lastd As Date)> If startdate> lastd Then ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: whats wrong with this function?
    ... Well, you haven't shown any code that uses the .ToString() method, so is ... For what you have, you need a Sub. ... Sub HideLink (val As String) ...
    (microsoft.public.dotnet.general)
  • Re: Formatting with Decimal.ToString() ... CultureInfo and DigitGrouping...
    ... Private Sub TextBox1_Leave(ByVal sender As Object, ... passing in the parameter of the ToString() like this. ... custom format string passed to Decimal's ToString. ... >> Regards, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Need help with hiding LinkButton in DataList
    ... > toString() to convert the CommandArgument contents to a text string... ... >> I need help with datalist and linkbutton. ... >> End Sub ... e As DataListCommandEventArgs) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: simple Boxing question
    ... needs to implicitly box the int before calling the overridden ToString ... I read that if one overrides the ToStringmethod ...
    (microsoft.public.dotnet.languages.csharp)