Hi
I have a class which uses Convert.ToString(o, CultureInfo.InvariantCulture), to convert the "object" o to a string. (This works fine for ints and doubles for example).
However, when I pass a custom class as 'o' I can't seem to get the results I want. I override ToString - and this is called. But how do I tell "Convert" to call my custom class method which is called ToString(IFormatProvider fp)?
Thanks,
Peter
Loading
VulpesPosted Sep 27, 2012, 2:23 PM
http://msdn.microsoft.com/en-us/library/ms131014(VS.100).aspx
That makes sense as there will be no way of knowing whether the custom class has an override of ToString() which takes an IFormatProvider parameter.
If you're going to always use the invariant culture, then you could add an additional overload of ToString() to your custom class which takes no parameters:
public override string ToString()
{
return ToString(CultureInfo.InvariantCulture);
}
public string ToString(IFormatProvider fp)
{
// existing code
}
PeterPosted Sep 27, 2012, 2:28 PM