364,112 Members | 2289 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

ToString on Object using Invariant Culture

Phil
P: n/a
Phil
I have a variable defined as an Object type, which may contain a Double,
Integer, String or another simple type.
All of these types have an overload for ToString which takes a CultureInfo
as a parameter, so I can specify CultureInfo.InvariantCulture.
The Object type itself though does not have this overload so I can't just do
this:

MyString = MyObj.ToString(CultureInfo.InvariantCulture)

because it won't compile.
I have managed to work around this problem like this:

MyString = CallByName(MyObj, "ToString", CallType.Method,
CultureInfo.InvariantCulture)

but I feel there ought to be a 'better' way to do this.
Any suggestions appreciated.
TIA
Phil.


Nov 13 '08 #1
Share this Question
Share on Google+
5 Replies


Armin Zingler
P: n/a
Armin Zingler
"Phil" <N/Aschrieb
I have a variable defined as an Object type, which may contain a
Double, Integer, String or another simple type.
All of these types have an overload for ToString which takes a
CultureInfo as a parameter, so I can specify
CultureInfo.InvariantCulture.
The Object type itself though does not have this overload so I can't
just do this:
>
MyString = MyObj.ToString(CultureInfo.InvariantCulture)
>
because it won't compile.
I have managed to work around this problem like this:
>
MyString = CallByName(MyObj, "ToString", CallType.Method,
CultureInfo.InvariantCulture)
>
but I feel there ought to be a 'better' way to do this.
Any suggestions appreciated.
TIA
Phil.

Declare the variable as IFormattable. Though, String does not implement this
interface, but for the String type, it doesn't make sense to call this
overloaded ToString version anyway because it doesn't do a conversion.


Armin

Nov 13 '08 #2

Phil
P: n/a
Phil
Declare the variable as IFormattable. Though, String does not implement
this
interface, but for the String type, it doesn't make sense to call this
overloaded ToString version anyway because it doesn't do a conversion.
That sounds good. I can do something like this:

If TypeOf MyObj Is IFormattable Then
MyString = CType(MyObj, IFormattable).ToString(Nothing,
CultureInfo.InvariantCulture)
Else
MyString = MyObject.ToString
End If



Nov 13 '08 #3

Phil
P: n/a
Phil
>Declare the variable as IFormattable. Though, String does not implement
>this
>interface, but for the String type, it doesn't make sense to call this
>overloaded ToString version anyway because it doesn't do a conversion.
>
That sounds good. I can do something like this:
>
If TypeOf MyObj Is IFormattable Then
MyString = CType(MyObj, IFormattable).ToString(Nothing,
CultureInfo.InvariantCulture)
Else
MyString = MyObject.ToString
End If
>
Actually String *does* implement this overload (does no conversion though),
so I don't think I need the check on the object type at all (I know for this
application that it will always be a simple generic type, not an array nor a
class instance).


Nov 13 '08 #4

Armin Zingler
P: n/a
Armin Zingler
"Phil" <N/Aschrieb
Actually String *does* implement this overload
Yes, but String does not implement IFormattable.
(does no conversion
though), so I don't think I need the check on the object type at all
(I know for this application that it will always be a simple generic
type, not an array nor a class instance).

Armin

Nov 13 '08 #5

Phil
P: n/a
Phil
>Actually String *does* implement this overload
>
Yes, but String does not implement IFormattable.
>
I see, yes :-)
I think though I can use IConvertible instead.
This is implemented by all the simple generic types I think, and provides a
ToString overload that I can pass in a CultureInfo.
I don't actually need to use any special formatting. I just want to make
sure these values are stored in the same way irrespective of the user's
regional language settings.



Nov 13 '08 #6

Post your reply

Help answer this question



Didn't find the answer to your Visual Basic .NET question?

You can also browse similar questions: Visual Basic .NET tostring invariant