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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary James

    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,


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

    #2
    Re: Error: No overload for method 'ToString' takes '1' arguments

    Hi,


    "Gary James" <removethis.gar yj@iotech.comwr ote in message
    news:O3CV4pj9HH A.5360@TK2MSFTN GP03.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


    Comment

    • Peter Duniho

      #3
      Re: Error: No overload for method 'ToString' takes '1' arguments

      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

      Comment

      • Chris Shepherd

        #4
        Re: Error: No overload for method 'ToString' takes '1' arguments

        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("som ewhackyformat") ;
        }

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

        Chris.

        Comment

        • Gary James

          #5
          Re: Error: No overload for method 'ToString' takes '1' arguments

          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" <chsh@nospam.ch sh.cawrote in message
          news:OGOJzjk9HH A.2004@TK2MSFTN GP06.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("som ewhackyformat") ;
          }
          >
          Or did you mean something else by "nullable type" (as in, reference
          types)?
          >
          Chris.

          Comment

          • Chris Shepherd

            #6
            Re: Error: No overload for method 'ToString' takes '1' arguments

            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;Ne gative;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.

            Comment

            • Gary James

              #7
              Re: Error: No overload for method 'ToString' takes '1' arguments


              Custom formats are described at:


              Standard formats are described at:


              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.D ouble))
              {
              double db = Convert.ToDoubl e(_val);
              _host.Text = db.ToString(_fo rmat);
              }

              else if (_val.GetType() == typeof(System.S ingle))
              {
              float fl = Convert.ToSingl e(_val);
              _host.Text = fl.ToString(_fo rmat);
              }

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

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

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

              but I was hoping there was a better way.

              Gary ...




              "Chris Shepherd" <chsh@nospam.ch sh.cawrote in message
              news:OtjyJqs9HH A.600@TK2MSFTNG P05.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.ToStrin g() 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;Ne gative;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.

              Comment

              • Doug Semler

                #8
                Re: Error: No overload for method 'ToString' takes '1' arguments

                On Sep 14, 8:56 am, "Gary James" <removethis.ga. ..@iotech.comwr ote:
                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.D ouble))
                {
                double db = Convert.ToDoubl e(_val);
                _host.Text = db.ToString(_fo rmat);
                >
                }
                >
                else if (_val.GetType() == typeof(System.S ingle))
                {
                float fl = Convert.ToSingl e(_val);
                _host.Text = fl.ToString(_fo rmat);
                >
                }
                >
                else if (_val.GetType() == typeof(System.I nt16))
                {
                Int16 i16 = Convert.ToInt16 (_val);
                _host.Text = i16.ToString(_f ormat);
                >
                }
                >
                else if (_val.GetType() == typeof(System.I nt32))
                {
                Int32 i32 = Convert.ToInt32 (_val);
                _host.Text = i32.ToString(_f ormat);
                >
                }
                >
                else if (_val.GetType() == typeof(System.I nt64))
                {
                Int64 i64 = Convert.ToInt64 (_val);
                _host.Text = i64.ToString(_f ormat);
                >
                }
                >
                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 realFormatStrin g = "{0:" + format + "}";
                return string.Format(r ealFormatString , 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);
                }
                }



                Comment

                • Chris Shepherd

                  #9
                  Re: Error: No overload for method 'ToString' takes '1' arguments

                  Gary James wrote:
                  Custom formats are described at:

                  >
                  Standard formats are described at:

                  >
                  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.

                  Comment

                  • Gary James

                    #10
                    Re: Error: No overload for method 'ToString' takes '1' arguments

                    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" <chsh@nospam.ch sh.cawrote in message
                    news:%23UR55Yt9 HHA.600@TK2MSFT NGP05.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(forma t) 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.

                    Comment

                    • Terry Rogers

                      #11
                      Re: Error: No overload for method 'ToString' takes '1' arguments

                      On Sep 14, 12:30 pm, "Gary James" <removethis.ga. ..@iotech.comwr ote:
                      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(IForma ttable number)
                      {
                      return number.ToString ("Format String");
                      }


                      Comment

                      • Chris Shepherd

                        #12
                        Re: Error: No overload for method 'ToString' takes '1' arguments

                        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.

                        Comment

                        Working...