String to Single

  • Thread starter Thread starter Olie
  • Start date Start date
O

Olie

A user of my software is getting this exception:

Conversion from string "0.5" to type 'Single' is not valid.

I have about 30 other users who do not get this exception when running
the same code and I have never seen it on my development PCs. As far
as I can see there is nothing wrong with converting "0.5" to a number.
Does anyone have any idea why he might experience this problem and no
one else?

He is running Windows in English with the keyboard settings, set to
Swedish.
 
Olie said:
A user of my software is getting this exception:

Conversion from string "0.5" to type 'Single' is not valid.

I have about 30 other users who do not get this exception when
running the same code and I have never seen it on my development
PCs. As far as I can see there is nothing wrong with converting
"0.5" to a number. Does anyone have any idea why he might experience
this problem and no one else?

He is running Windows in English with the keyboard settings, set to
Swedish.

The OS language and the keyboard settings is not important. More important
are the regional settings.

What's the source of the string "0.5"? From a file? User input?


Armin
 
He tells me the regional settings are set to English. The string is
being read from an Xml file using the code:

Dim zoom As Single

zoom = XmlNode.Attributes("Zoom").Value

I do not think I used Single.Parse() but maybe I need to. I do not
have the code in front of me so this is from memory.

Does it matter where the source comes from. Surely a string is a
string wherever it came from.
 
He can not have the regional settings set to English. I just tried
running the software with the regional settings set to Finnish and it
gives you exactly the error he mentions.

Thanks for the hint! Do you know how to fix this? Will Single.Parse()
deal with this?
 
Olie said:
He can not have the regional settings set to English. I just tried
running the software with the regional settings set to Finnish and
it gives you exactly the error he mentions.

Thanks for the hint! Do you know how to fix this? Will
Single.Parse() deal with this?

You can pass System.Globalization.CultureInfo.InvariantCulture.Numberformat
as an additional parameter to Single.Parse.

It's strongly recommended to enable Option Strict in order to get aware of
these problems ASAP. Obviously you set it Off, otherwise this couldn't
be compiled (value property is of type String):

Dim zoom As Single

zoom = XmlNode.Attributes("Zoom").Value


Armin
 
The project was exported from an older version of Visual Studio when
the Option Strict was set to Off by default. I agree it is a good idea
to turn it on.

Am I right in thinking that

Single.Parse("0.5")

would still fail on a computer with different language settings. I am
reading the value in from a config file where the number is set to
"0.5" so do I have to call Parse and specifically say to use the UK
number format.

Thanks so much for all your help!
 
Olie said:
The project was exported from an older version of Visual Studio when
the Option Strict was set to Off by default. I agree it is a good
idea to turn it on.

Am I right in thinking that

Single.Parse("0.5")

would still fail on a computer with different language settings. I
am reading the value in from a config file where the number is set
to "0.5" so do I have to call Parse and specifically say to use the
UK number format.

Yes, the InvariantCulture ensures that it can be converted independent from
the current culture setting that is used by default.


Armin
 
Armin,
Yes, the InvariantCulture ensures that it can be converted independent
from the current culture setting that is used by default.
Are you sure of that, I have read somewhere that the InvariantCulture is for
the differences in the English language, however not for Dutch and German.

Cor
 
Cor Ligthert said:
Armin,

Are you sure of that, I have read somewhere that the
InvariantCulture is for the differences in the English language,
however not for Dutch and German.

Which differences? I don't understand.


Armin
 
Cor Ligthert said:
Are you sure of that, I have read somewhere that the InvariantCulture is
for the differences in the English language, however not for Dutch and
German.

The invariant culture is intended for exchange of data between multiple
machines regardless of their culture settings (it is based on US
conventions, but it is in no way semantically related to the 'en-US'
culture). Thus it is very suitable for protocols and writing data to files
and streams.
 
English has some differences between the majority of its users and the
official language.

AFAIK is the way the Americans are using the date is the classic way as used
in 18/19 century (I assume before the time that dates were written only in
figurs). The other English speakers do it now the same like we do. (You know
this of course however for the message)

I thought that this was the InvariantCulture. In other words december 5
(th) and 5 (th of) december.

Cor
 
Cor Ligthert said:
English has some differences between the majority of its users and
the official language.

AFAIK is the way the Americans are using the date is the classic way
as used in 18/19 century (I assume before the time that dates were
written only in figurs). The other English speakers do it now the
same like we do. (You know this of course however for the message)

I thought that this was the InvariantCulture. In other words
december 5 (th) and 5 (th of) december.


Ok. Well, I could only add what Herfried wrote in his latest post.


Armin
 
The invariant culture is intended for exchange of data between multiple
machines regardless of their culture settings (it is based on US
conventions, but it is in no way semantically related to the 'en-US'
culture). Thus it is very suitable for protocols and writing data to
files and streams.
Did I write that, I had the idea that I was writing about the English
language, not about the English culture of American culture. In all
cultures where English is spoken is the dot the dot the decimal seperator.

I have searched again for that text

The invariant culture is culture-insensitive. You can specify the invariant
culture by name using an empty string ("") or by its language identifier.
InvariantCulture retrieves an instance of the invariant culture. It is
associated with the English language but not with any country/region. It can
be used in almost any method in the Globalization namespace that requires a
culture.

Cor
 
Olie,

Is that strange as AFAIK the InvariantCulture is an English language non
specific Culture, I would be more supprised as it was the other way around ?

Probably it works with nothing as well.

Cor
 
Back
Top