473,769 Members | 6,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't override CultureInfo.ToS tring

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.Nat iveName
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.Globaliz ation

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.EventArg s) 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
8 2834
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
Thanks a lot. I have now used the code

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

instead of

Public Overrides Function ToString() As String
Return MyBase.NativeNa me()
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.jpwr ote in message
news:%2******** ********@TK2MSF TNGP02.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
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.ToStrin g()")
Debug.WriteLine (item.ToString( ), "CultureInfo.To String()")

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

Have you considered simply:

Protected Overrides Sub OnLoad(ByVal e As System.EventArg s)
MyBase.OnLoad(e )
ListBox1.Items. Add(New CultureInfo("de "))
ListBox1.Items. Add(New CultureInfo("es "))
ListBox1.Items. Add(New CultureInfo("fr "))
ListBox1.Displa yMember = "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.Nat iveName
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.Globaliz ation

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.EventArg s) 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
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
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
CultureInfoConv erter 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.Nat iveName
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.Globaliz ation

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.EventArg s) 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
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
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****@newsgro ups.nospamwrote in message
news:13******** *************** ***********@mic rosoft.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
CultureInfoConv erter 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.Nat iveName
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.Globaliz ation

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.EventAr gs) 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1318
by: Patrick De Ridder | last post by:
Is it possible to change this function into a string override? So I could just say d.ToString() Patrick private string reformat(double d) { NumberFormatInfo nfi = new CultureInfo("").NumberFormat; nfi.NumberDecimalSeparator="."; nfi.NumberGroupSeparator="";
0
1500
by: Karunakararao | last post by:
Hi Vinay I am not able to get the web application. regional settings made at the operating system level. For example, Canada uses a dd/mm/yyyy format while the US uses mm/dd/yyyy. and yyyy/mm/dd when i am changing the short date format in regional settings That is not effected . when i was change the like engilsh any other german it is effecting the date formats. how can i get this type date format :
7
4561
by: AWHK | last post by:
How can I force anyone who subclasses my class to override the ToString() method? Andreas :-)
5
6926
by: Jason L James | last post by:
Hi have two subroutines that change the currentCulture property of my application. I can call either: System.Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB") or:
8
9180
by: G.Ashok | last post by:
Hi, I have created CultureInfo object and specified required digit grouping in it. The one of the overloaded ToString methods of Decimal type has parameters to format the value with required custom format and a IFormatProvider. I pass a custom format string for positive, negative and Zero (3 sections) and CultureInfo object containing the required DigitGrouping as IFormatProvider. But ToString is not formatting the value using the digit...
7
8092
by: bojan.pikl | last post by:
Hi, I am making a calendar. It is costum made and I would like to have the ability to choose the first day (Monday or Sunday). I know for the firstDayOfWeek, but I can't change it. What should I do? I tried this but it does not work (it is always Sunday): using System.Globalization; .... CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
8
6143
by: Rico | last post by:
Hello Everyone, I observed something strange in some quick testing code: void Button1Click(object sender, System.EventArgs e) { CultureInfo culture = new CultureInfo("th-TH", false); tbxDisplayResult.Text = DateTime.Parse(tbxInputArea.Text, culture).ToString("yyyy MMMMMMMMMM dd");
5
2392
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member object of generic type T, that supports IEquatable<T>, and a method, DoComparisons(T obj) to compare the member object to the object passed in.
4
14182
JustRun
by: JustRun | last post by:
Hi, I'm trying to develop a MultiLanguage web site using ASP.NET 2.0 with C# My problem is: I want to make my MasterPage support switching among languages, but when i put the "InitializeCulture()" inside the masterpage.cs, I got this erreor: MasterPage.InitializeCulture()': no suitable method found to override Note the following code works fine with xxx.aspx page, but not with MasterPage Here is my code: public override void...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.