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
Convert.ToString custom class
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

VulpesRank #55 · 29.2K reputationPosted Sep 27, 2012, 10:23 AM EDT
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
}
Peter14 reputationPosted Sep 27, 2012, 10:28 AM EDT