473,382 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ToString on Object using Invariant Culture

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
5 7340
"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
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
>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
"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
>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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
9
by: Iwan Petrow | last post by:
Hi, I have a string which represent a date as follow: "dd-.MM-.yyyy hh:mm". How could I convert this string to DateTime type (I prefer somehow to set this as a pattern if it is possible because...
4
by: John A Grandy | last post by:
what's the full class hierarchy for the CultureInfo object in effect by default ?
8
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...
1
by: Tom | last post by:
I need to modify the default behavior of Date.ToString() on all pages of my ASP.Net (2.0) site. I don't need to localize my app (it's an intranet-only site), but I need to enforce a specific...
12
by: ThunderMusic | last post by:
Hi, We have a part of our application that deals with millions of records and do some processing of them. We've achieved a pretty good performance gain by developping a custom DateTime.ToString...
5
by: Nick Gilbert | last post by:
Hi, One of our servers is behaving differently to all the others when printing dates. They seem to be outputting in American format (12 hour) when the default for a UK machine should be UK...
11
by: Gary James | last post by:
I'm using an object data type variable to pass a numeric value (Int, Float, Double, etc) to a function that returns a formatted string. However, nullable types do not provide an overridden...
2
by: helveticus | last post by:
Is there a way to keep datetime expressions culture invariant? My app is culture dependent and contains a series of datetime settings that are saved in cookies. The cookie date strings raise...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.