469,275 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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

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,
Sep 13 '07 #1
11 27012
Hi,
"Gary James" <re**************@iotech.comwrote in message
news:O3**************@TK2MSFTNGP03.phx.gbl...
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
Sep 13 '07 #2
Gary James wrote:
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
Sep 13 '07 #3
Gary James wrote:
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.
Sep 13 '07 #4
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.

"Chris Shepherd" <ch**@nospam.chsh.cawrote in message
news:OG**************@TK2MSFTNGP06.phx.gbl...
Gary James wrote:
>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.

Sep 14 '07 #5
Gary James wrote:
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;Negativ e;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.
Sep 14 '07 #6

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

Standard formats are described at:
http://msdn2.microsoft.com/en-us/lib...9k(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" <ch**@nospam.chsh.cawrote in message
news:Ot*************@TK2MSFTNGP05.phx.gbl...
Gary James wrote:
>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;Negativ e;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.

Sep 14 '07 #7
On Sep 14, 8:56 am, "Gary James" <removethis.ga...@iotech.comwrote:
Custom formats are described at:http://msdn2.microsoft.com/en-us/lib...k8(VS.90).aspx

Standard formats are described at:http://msdn2.microsoft.com/en-us/lib...9k(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);
}
}

Sep 14 '07 #8
Gary James wrote:
Custom formats are described at:
http://msdn2.microsoft.com/en-us/lib...k8(VS.90).aspx

Standard formats are described at:
http://msdn2.microsoft.com/en-us/lib...9k(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.
Sep 14 '07 #9
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" <ch**@nospam.chsh.cawrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
Gary James wrote:
>Custom formats are described at:
http://msdn2.microsoft.com/en-us/lib...k8(VS.90).aspx

Standard formats are described at:
http://msdn2.microsoft.com/en-us/lib...9k(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.

Sep 14 '07 #10
On Sep 14, 12:30 pm, "Gary James" <removethis.ga...@iotech.comwrote:
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");
}
Sep 14 '07 #11
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.
Sep 14 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Eph0nk | last post: by
3 posts views Thread by portroe | last post: by
26 posts views Thread by Martin Jørgensen | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.