Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gary James - 13 Sep 2007 20:15 GMT
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,
Ignacio Machin ( .NET/ C# MVP ) - 13 Sep 2007 21:07 GMT
Hi,

> 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
Peter Duniho - 13 Sep 2007 21:55 GMT
> 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,
[quoted text clipped - 3 lines]
> 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
Chris Shepherd - 13 Sep 2007 21:59 GMT
> 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,
[quoted text clipped - 3 lines]
> 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.
Gary James - 14 Sep 2007 12:30 GMT
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.

>> 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.
[quoted text clipped - 18 lines]
>
> Chris.
Chris Shepherd - 14 Sep 2007 13:26 GMT
> 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.
Gary James - 14 Sep 2007 13:56 GMT
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 ...

>> 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
[quoted text clipped - 27 lines]
>
> Chris.
Doug Semler - 14 Sep 2007 14:41 GMT
> Custom formats are described at:http://msdn2.microsoft.com/en-us/library/0c899ak8(VS.90).aspx
>
[quoted text clipped - 39 lines]
>
> 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);
       }
   }
Chris Shepherd - 14 Sep 2007 14:50 GMT
> Custom formats are described at:
> http://msdn2.microsoft.com/en-us/library/0c899ak8(VS.90).aspx
[quoted text clipped - 4 lines]
> 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.
Gary James - 14 Sep 2007 15:29 GMT
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 ...

>> Custom formats are described at:
>> http://msdn2.microsoft.com/en-us/library/0c899ak8(VS.90).aspx
[quoted text clipped - 30 lines]
>
> Chris.
Chris Shepherd - 14 Sep 2007 15:33 GMT
> 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.
Terry Rogers - 14 Sep 2007 15:35 GMT
> 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
[quoted text clipped - 10 lines]
>
> 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");
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.