Collection key regional settings german umlaute

Hi newsgroup,

greetings from Germany. I've a simple vb.net 2005 code-snippet:

Dim xAsNew Collection

x.Add(0,"Müller")

x.Add(0,"Mueller")

I found out, that sometimes the snippet runs (as compiled exe) without any problems, sometimes not.

Adding an item with the key Mueller, when the key Müller exists, may raise an exception. It depends on the regional settings (German Windows XP Regions- und Sprachoptionen).

The sort-option Telefonbuch (DIN) raises the error, W?rterbuch doesn't.

Is this behaviour a bug? I think so, because a string should not be converted into a system string using a special sort-setting before it is used in a collection key.

What do you think about it?

Sorry, but I don't know the english words for the options.

Thank you for your answers!

[1036 byte] By [migeold] at [2007-12-24]
# 1

Are you using the Microsoft.VisualBasic.Collection class? If so, My personal recommendation that you switch to a collection class that gives you a bit more control over what you are doing. VB6 was greate in many ways, but it wasn't always easy to work with culture-aware thingies. The .NET Framework is *much* better at this.

Try the System.Collections.Generic.SortedList class . You can pass in the StringComparer that you want to control what strings are supposed to be treated as "equal".

Best regards,
Johan Stenberg

MSJohanStenberg at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Hi Johan,

thank you very much for your help!

I am using the VisualBasic.Collection class.

Please, could you give me an hint how I can pass the StringComparer in the SortedList.

SortedList.Comparer is readonly.

Thanks, migeold.

migeold at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

When you create the list you can specify the stringComparer: e.g.

Dim L As New SortedList(Of String, String)(StringComparer.CurrentCultureIgnoreCase)

(the above should be on a single line...)

SJWhiteley at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Dim x As New System.Collections.Generic.SortedList(Of String, String)(StringComparer.InvariantCulture)

The InvariantCulture solves my problem. The sort setting from the operating system is ignored.

Thank you very much, Johan and Stephen!

Migeold.
migeold at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...