IsNumeric: Convert.ToInt32 vs. Convert.ToInt64

Discussion in 'ASP .Net' started by sck10, Sep 2, 2006.

  1. sck10

    sck10 Guest

    Hello,

    I am trying to test for IsNumeric using C# with the following:

    public bool IsNumeric(object TestValue)
    {
    try
    {
    int i = Convert.ToInt32(TestValue.ToString());
    return true;
    }
    catch (FormatException)
    {
    return false;
    }
    }


    If I use ToInt32, I get the error: "System.OverflowException: Value was
    either too large or too small for an Int32." For a value of 9999999999

    If I try: int i = Convert.ToInt64(TestValue.ToString());

    Cannot implicitly convert type 'long' to 'int'. An explicit conversion
    exists (are you missing a cast?)

    Any assistance on why Convert.ToInt64 is failing would be appreciated.
    Thanks, sck10
     
    sck10, Sep 2, 2006
    #1
    1. Advertising

  2. sck10

    Mark Rae Guest

    "sck10" <> wrote in message
    news:OyAVo%...

    > If I try: int i = Convert.ToInt64(TestValue.ToString());
    >
    > Cannot implicitly convert type 'long' to 'int'. An explicit conversion
    > exists (are you missing a cast?)
    >
    > Any assistance on why Convert.ToInt64 is failing would be appreciated.


    Er, because you're trying to plug an Int64 into an Int32... :)

    Would this work...?
    byte i = Convert.ToInt64(TestValue.ToString());

    Why not...?

    For the same reason, neither will:
    int i = Convert.ToInt64(TestValue.ToString());

    This will, though...
    Int64 i = Convert.ToInt64(TestValue.ToString());
     
    Mark Rae, Sep 3, 2006
    #2
    1. Advertising

  3. Why are you still trying the exception catching approach? This question was
    answered the last time you posted a few hours previously.
    --
    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C#/VB to C++ converter
    C# Code Metrics: Quick metrics for C#


    "sck10" wrote:

    > Hello,
    >
    > I am trying to test for IsNumeric using C# with the following:
    >
    > public bool IsNumeric(object TestValue)
    > {
    > try
    > {
    > int i = Convert.ToInt32(TestValue.ToString());
    > return true;
    > }
    > catch (FormatException)
    > {
    > return false;
    > }
    > }
    >
    >
    > If I use ToInt32, I get the error: "System.OverflowException: Value was
    > either too large or too small for an Int32." For a value of 9999999999
    >
    > If I try: int i = Convert.ToInt64(TestValue.ToString());
    >
    > Cannot implicitly convert type 'long' to 'int'. An explicit conversion
    > exists (are you missing a cast?)
    >
    > Any assistance on why Convert.ToInt64 is failing would be appreciated.
    > Thanks, sck10
    >
    >
    >
     
    =?Utf-8?B?RGF2aWQgQW50b24=?=, Sep 3, 2006
    #3
  4. sck10

    sck10 Guest

    Thanks David,

    I am using the approach you presented earlier (from your website), but I was
    interested in learning why .ToInt64 was not working.

    Thanks again...


    "David Anton" <> wrote in message
    news:...
    > Why are you still trying the exception catching approach? This question
    > was
    > answered the last time you posted a few hours previously.
    > --
    > David Anton
    > www.tangiblesoftwaresolutions.com
    > Instant C#: VB to C# converter
    > Instant VB: C# to VB converter
    > Instant C++: C#/VB to C++ converter
    > C# Code Metrics: Quick metrics for C#
    >
    >
    > "sck10" wrote:
    >
    >> Hello,
    >>
    >> I am trying to test for IsNumeric using C# with the following:
    >>
    >> public bool IsNumeric(object TestValue)
    >> {
    >> try
    >> {
    >> int i = Convert.ToInt32(TestValue.ToString());
    >> return true;
    >> }
    >> catch (FormatException)
    >> {
    >> return false;
    >> }
    >> }
    >>
    >>
    >> If I use ToInt32, I get the error: "System.OverflowException: Value was
    >> either too large or too small for an Int32." For a value of 9999999999
    >>
    >> If I try: int i = Convert.ToInt64(TestValue.ToString());
    >>
    >> Cannot implicitly convert type 'long' to 'int'. An explicit conversion
    >> exists (are you missing a cast?)
    >>
    >> Any assistance on why Convert.ToInt64 is failing would be appreciated.
    >> Thanks, sck10
    >>
    >>
    >>
     
    sck10, Sep 3, 2006
    #4
  5. sck10

    daniel Guest

    I recommend to use a regular expression to IsNumeric Method, is more
    eficcient.

    regards
    daniel

    "sck10" <> wrote in message
    news:OyAVo%...
    > Hello,
    >
    > I am trying to test for IsNumeric using C# with the following:
    >
    > public bool IsNumeric(object TestValue)
    > {
    > try
    > {
    > int i = Convert.ToInt32(TestValue.ToString());
    > return true;
    > }
    > catch (FormatException)
    > {
    > return false;
    > }
    > }
    >
    >
    > If I use ToInt32, I get the error: "System.OverflowException: Value was
    > either too large or too small for an Int32." For a value of 9999999999
    >
    > If I try: int i = Convert.ToInt64(TestValue.ToString());
    >
    > Cannot implicitly convert type 'long' to 'int'. An explicit conversion
    > exists (are you missing a cast?)
    >
    > Any assistance on why Convert.ToInt64 is failing would be appreciated.
    > Thanks, sck10
    >
     
    daniel, Sep 3, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    1
    Views:
    405
    Karl Seguin
    Jul 23, 2003
  2. Andreas Klemt
    Replies:
    4
    Views:
    521
    John Saunders
    Jul 24, 2003
  3. Stephan Bour

    IsNumeric C# equivalent

    Stephan Bour, Nov 5, 2003, in forum: ASP .Net
    Replies:
    12
    Views:
    244,082
    daniefvh
    Jan 26, 2012
  4. martin
    Replies:
    3
    Views:
    10,639
    Kevin Spencer
    Jul 19, 2004
  5. =?Utf-8?B?TXJNaWtl?=

    IsNumeric Function

    =?Utf-8?B?TXJNaWtl?=, Nov 29, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    4,887
    Dan Bass
    Nov 29, 2004
Loading...

Share This Page