Error: No overload for method 'ToString' takes '1' arguments

G

Gary James

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 ToString() method that takes a
format string.

Anyone have any suggestions how to approach this problem to allow a custom
format string with an object data type?

Thanks,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Gary James said:
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 ToString() method
that takes a format string.

You can check if the instance if a nullable type, if so you can check if it
has a value if so call ToString on it

Of course you will have to use Reflection. But the code should be easy
 
P

Peter Duniho

Gary said:
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 ToString() method that takes a
format string.

Anyone have any suggestions how to approach this problem to allow a custom
format string with an object data type?

I'm not sure I understand the question exactly. For one, you mention
using a variable typed as "object", but then you also mention using a
nullable type. Are you passing a nullable type boxed as an "object"?

In any case, I would expect the string.Format() method to be useful.
You can pass a formatting string and an object reference, which should
generally produce the same results as using ToString() with a formatting
string on the object itself.

It would surprise me if using reflection was the best way to deal with
whatever issue you are having. Not that I haven't been surprised in the
past, but still... :)

Pete
 
C

Chris Shepherd

Gary said:
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 ToString() method that takes a
format string.

Anyone have any suggestions how to approach this problem to allow a custom
format string with an object data type?

If you need to handle Nullable<T>, could you not just do something like:

public void doWork(object o)
{
if (o is Nullable)
// Whatever for nulls
else
o.ToString("somewhackyformat");
}

Or did you mean something else by "nullable type" (as in, reference types)?

Chris.
 
G

Gary James

I appologize for not being as clear as I should have. I have a function
that takes an object (not by reference) as a argument. The object can
contain any one of the standard numerical data types: float, double, int,
uint, etc. I want the function to perform string formatting on the number
using CUSTOM format strings, not C# Standard format strings. Since the
number is passed as an object, not a numerical data type, the
Object.ToString() function does not have an overload to accept a format
string. Also, the String.Format() function does not accept CUSTOM format
strings, only Standard format strings.

What I'm looking for is an example of how I can use a custom format string
to format a numerical data type passed as an object.

Thanks again.
 
C

Chris Shepherd

Gary said:
I appologize for not being as clear as I should have. I have a function
that takes an object (not by reference) as a argument. The object can
contain any one of the standard numerical data types: float, double, int,
uint, etc. I want the function to perform string formatting on the number
using CUSTOM format strings, not C# Standard format strings. Since the

If you're stuck using numeric only types like this, why not simply
overload the method to accept the various types you want to except, and
offer nothing for everything else?
number is passed as an object, not a numerical data type, the
Object.ToString() function does not have an overload to accept a format
string. Also, the String.Format() function does not accept CUSTOM format
strings, only Standard format strings.

That's not true, unless you mean something totally different when you
say CUSTOM format strings. Try this code:

static void Main(string[] args)
{
object o = 2.45;
//o = 0;
//o = -50;
MessageBox.Show(String.Format("{0:positive;Negative;Zero}", o));
}

The message I get when o is 2.45 is "Positive", and when o is 0 I get
"Zero", and negative I get "Negative" -- it seems to work fine.

Chris.
 
G

Gary James

Custom formats are described at:
http://msdn2.microsoft.com/en-us/library/0c899ak8(VS.90).aspx

Standard formats are described at:
http://msdn2.microsoft.com/en-us/library/dwhawy9k(VS.90).aspx

In either case the Object data type does not support a native overloaded
ToString(format) method. Yes I can do the following:

if (_val.GetType() == typeof(System.Double))
{
double db = Convert.ToDouble(_val);
_host.Text = db.ToString(_format);
}

else if (_val.GetType() == typeof(System.Single))
{
float fl = Convert.ToSingle(_val);
_host.Text = fl.ToString(_format);
}

else if (_val.GetType() == typeof(System.Int16))
{
Int16 i16 = Convert.ToInt16(_val);
_host.Text = i16.ToString(_format);
}

else if (_val.GetType() == typeof(System.Int32))
{
Int32 i32 = Convert.ToInt32(_val);
_host.Text = i32.ToString(_format);
}

else if (_val.GetType() == typeof(System.Int64))
{
Int64 i64 = Convert.ToInt64(_val);
_host.Text = i64.ToString(_format);
}

but I was hoping there was a better way.

Gary ...




Chris Shepherd said:
Gary said:
I appologize for not being as clear as I should have. I have a function
that takes an object (not by reference) as a argument. The object can
contain any one of the standard numerical data types: float, double, int,
uint, etc. I want the function to perform string formatting on the
number using CUSTOM format strings, not C# Standard format strings.
Since the

If you're stuck using numeric only types like this, why not simply
overload the method to accept the various types you want to except, and
offer nothing for everything else?
number is passed as an object, not a numerical data type, the
Object.ToString() function does not have an overload to accept a format
string. Also, the String.Format() function does not accept CUSTOM format
strings, only Standard format strings.

That's not true, unless you mean something totally different when you say
CUSTOM format strings. Try this code:

static void Main(string[] args)
{
object o = 2.45;
//o = 0;
//o = -50;
MessageBox.Show(String.Format("{0:positive;Negative;Zero}", o));
}

The message I get when o is 2.45 is "Positive", and when o is 0 I get
"Zero", and negative I get "Negative" -- it seems to work fine.

Chris.
 
D

Doug Semler

Custom formats are described at:http://msdn2.microsoft.com/en-us/library/0c899ak8(VS.90).aspx

Standard formats are described at:http://msdn2.microsoft.com/en-us/library/dwhawy9k(VS.90).aspx

In either case the Object data type does not support a native overloaded
ToString(format) method. Yes I can do the following:

if (_val.GetType() == typeof(System.Double))
{
double db = Convert.ToDouble(_val);
_host.Text = db.ToString(_format);

}

else if (_val.GetType() == typeof(System.Single))
{
float fl = Convert.ToSingle(_val);
_host.Text = fl.ToString(_format);

}

else if (_val.GetType() == typeof(System.Int16))
{
Int16 i16 = Convert.ToInt16(_val);
_host.Text = i16.ToString(_format);

}

else if (_val.GetType() == typeof(System.Int32))
{
Int32 i32 = Convert.ToInt32(_val);
_host.Text = i32.ToString(_format);

}

else if (_val.GetType() == typeof(System.Int64))
{
Int64 i64 = Convert.ToInt64(_val);
_host.Text = i64.ToString(_format);

}

but I was hoping there was a better way.

I'm missing something? I thought String.Format took custom format
strings...What about something as simple as:

class Program
{
static string FuncTest(string format, object obj)
{
// Parameter check here - format not null, object not
null, object type valid, etc...
string realFormatString = "{0:" + format + "}";
return string.Format(realFormatString, obj);
}

static void Main(string[] args)
{
double MyPos = 19.95234, MyNeg = -19.9551325, MyZero =
0.0;

// In the U.S. English culture, MyString has the value:
$19.95.
string MyString = FuncTest("$#,##0.00;($#,##0.00);Zero",
MyPos);

// In the U.S. English culture, MyString has the value:
($19.96).
// The minus sign is omitted by default.
MyString = FuncTest("$#,##0.00;($#,##0.00);Zero", MyNeg);

// In the U.S. English culture, MyString has the value:
Zero.
MyString = FuncTest("$#,##0.00;($#,##0.00);Zero", MyZero);

int iMyPos = 19, iMyNeg = -19, iMyZero = 0;

// In the U.S. English culture, MyString has the value:
$19.00.
MyString = FuncTest("$#,##0.00;($#,##0.00);Zero", iMyPos);

// In the U.S. English culture, MyString has the value:
($19.00).
// The minus sign is omitted by default.
MyString = FuncTest("$#,##0.00;($#,##0.00);Zero", iMyNeg);

// In the U.S. English culture, MyString has the value:
Zero.
MyString = FuncTest("$#,##0.00;($#,##0.00);Zero",
iMyZero);
}
}
 
C

Chris Shepherd

Gary said:
Custom formats are described at:
http://msdn2.microsoft.com/en-us/library/0c899ak8(VS.90).aspx

Standard formats are described at:
http://msdn2.microsoft.com/en-us/library/dwhawy9k(VS.90).aspx

In either case the Object data type does not support a native overloaded
ToString(format) method. Yes I can do the following: [...]
but I was hoping there was a better way.

Gary ...

Actually, there is... I posted it in my previous post. It was the entire
lower half.

Also, I wouldn't do what you were showing there, rather, just make the
method overloaded with different data types as the argument:

public string someWork(Int32 num)

public string someWork(double num)

public string someWork(float num)

public string someWork(Int64 num)

And so on.

Still, given you can format it in a custom fashion, I don't get why you
still think it's a problem. I gave you a working code example displaying
the fact that String.Format *DOES* support custom formatting.

Chris.
 
G

Gary James

Ok guys, now I see what's happening. I was reading the description for
Custom and Standard string formatting too literally. This, combined with
the fact that the String.Format() function has several overloads for
multiple numbers of arguments, I was omitting the "{0:}" when I was using a
Custom format.

with _format = "#,##0.0", and _val = 2.56

_host.Text = string.Format(_format, _val);

resulted in _host.Text = "#,##0.0". I was getting the format string as the
result. I now realize that this was because overload number one for
String.Format() is:

Format(string format, object arg0).

And since this overload expected a single argument, and I was just
formatting a number without any other text in the format string, I thought I
didn't need to embed the {0:} in the format string.

_host.Text = string.Format("{0:" + _format + "}", _val);

works just as you described.

Sometimes you just can't see the forest because all those damn trees are in
the way.

A very humble Thanks again.

Gary ...


Chris Shepherd said:
Gary said:
Custom formats are described at:
http://msdn2.microsoft.com/en-us/library/0c899ak8(VS.90).aspx

Standard formats are described at:
http://msdn2.microsoft.com/en-us/library/dwhawy9k(VS.90).aspx

In either case the Object data type does not support a native overloaded
ToString(format) method. Yes I can do the following: [...]
but I was hoping there was a better way.

Gary ...

Actually, there is... I posted it in my previous post. It was the entire
lower half.

Also, I wouldn't do what you were showing there, rather, just make the
method overloaded with different data types as the argument:

public string someWork(Int32 num)

public string someWork(double num)

public string someWork(float num)

public string someWork(Int64 num)

And so on.

Still, given you can format it in a custom fashion, I don't get why you
still think it's a problem. I gave you a working code example displaying
the fact that String.Format *DOES* support custom formatting.

Chris.
 
C

Chris Shepherd

Sometimes you just can't see the forest because all those damn trees are in
the way.

A very humble Thanks again.

No worries, it happens to everyone. :)

Chris.
 
T

Terry Rogers

I appologize for not being as clear as I should have. I have a function
that takes an object (not by reference) as a argument. The object can
contain any one of the standard numerical data types: float, double, int,
uint, etc. I want the function to perform string formatting on the number
using CUSTOM format strings, not C# Standard format strings. Since the
number is passed as an object, not a numerical data type, the
Object.ToString() function does not have an overload to accept a format
string. Also, the String.Format() function does not accept CUSTOM format
strings, only Standard format strings.

What I'm looking for is an example of how I can use a custom format string
to format a numerical data type passed as an object.

Thanks again.

All the standard numeric types implement IFormattable interface, take
a reference of that type instead of object.

For example:
public string DoFormat(IFormattable number)
{
return number.ToString("Format String");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top