Import Microsoft.VisualBasic for IsNumeric?

Discussion in 'ASP .Net' started by =?Utf-8?B?RGF2ZQ==?=, Jan 16, 2006.

  1. I'm trying to import Microsoft.VisualBasic to use the IsNumeric function in
    my C# code but all I see is:

    Microsoft.VisualBasic.VBCodeProvider while using Intellisense...

    From samples shouldn't I see Microsoft.VisualBasic.Information.IsNumeric?

    Thanks
    =?Utf-8?B?RGF2ZQ==?=, Jan 16, 2006
    #1
    1. Advertising

  2. Sounds like you imported the wrong assembly.

    It's Microsoft.VisualBasic.dll that you want. This contains the .Information
    namespace, which has the IsNumeric method.
    I'm not even going to get into why it might be better not to do this, I'll
    just get flamed.

    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "Dave" wrote:

    > I'm trying to import Microsoft.VisualBasic to use the IsNumeric function in
    > my C# code but all I see is:
    >
    > Microsoft.VisualBasic.VBCodeProvider while using Intellisense...
    >
    > From samples shouldn't I see Microsoft.VisualBasic.Information.IsNumeric?
    >
    > Thanks
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 16, 2006
    #2
    1. Advertising

  3. After you imported Microsoft.VisualBasic namespace, when you type
    "IsNumeric(", does the Intellisense show up?

    And I wish to ask another question. For the OP's purpose, he can use the
    IsNumeric() function from Microsoft.VisualBasic namespace, or attempt to
    write one for himself with the following:

    public static bool InNumeric(string inputstr)
    {
    bool result = false;
    try
    {
    decimal temp = Convert.ToDecimal(inputstr);
    result = true;
    }
    catch
    {
    result = false;
    }
    return result;
    }

    Of course he may also determine the result by comparing the ASCII values of
    the string one by one for "0"-"9" and "." (possibly "e" and so on if
    applicable)

    For the above methods, which one is recommended to do in C#?

    "Dave" <> ¼¶¼g©ó¶l¥ó·s»D:...
    > I'm trying to import Microsoft.VisualBasic to use the IsNumeric function
    > in
    > my C# code but all I see is:
    >
    > Microsoft.VisualBasic.VBCodeProvider while using Intellisense...
    >
    > From samples shouldn't I see Microsoft.VisualBasic.Information.IsNumeric?
    >
    > Thanks
    Lau Lei Cheong, Jan 16, 2006
    #3
  4. The IsNumeric method from Microsoft.VisualBasic is not that simple. It does
    an incredible amount of complex type-checking with culture, Iconvertible,
    etc. under the hood.

    However, if you want a C# "Simplistic" version, I'd suggest using
    double.TryParse, which does not throw an exception.
    Peter


    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "Lau Lei Cheong" wrote:

    > After you imported Microsoft.VisualBasic namespace, when you type
    > "IsNumeric(", does the Intellisense show up?
    >
    > And I wish to ask another question. For the OP's purpose, he can use the
    > IsNumeric() function from Microsoft.VisualBasic namespace, or attempt to
    > write one for himself with the following:
    >
    > public static bool InNumeric(string inputstr)
    > {
    > bool result = false;
    > try
    > {
    > decimal temp = Convert.ToDecimal(inputstr);
    > result = true;
    > }
    > catch
    > {
    > result = false;
    > }
    > return result;
    > }
    >
    > Of course he may also determine the result by comparing the ASCII values of
    > the string one by one for "0"-"9" and "." (possibly "e" and so on if
    > applicable)
    >
    > For the above methods, which one is recommended to do in C#?
    >
    > "Dave" <> ¼¶¼g©ó¶l¥ó·s»D:...
    > > I'm trying to import Microsoft.VisualBasic to use the IsNumeric function
    > > in
    > > my C# code but all I see is:
    > >
    > > Microsoft.VisualBasic.VBCodeProvider while using Intellisense...
    > >
    > > From samples shouldn't I see Microsoft.VisualBasic.Information.IsNumeric?
    > >
    > > Thanks

    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 16, 2006
    #4
  5. In .Net 2.0, you can take advantage of the TryParse() implementations for
    various classes, without having to use Exception handling. Example:

    puvlic static bool IsNumeric(string input)
    {
    double d;
    return Double.TryParse(s, out d);
    }

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    "Lau Lei Cheong" <> wrote in message
    news:...
    > After you imported Microsoft.VisualBasic namespace, when you type
    > "IsNumeric(", does the Intellisense show up?
    >
    > And I wish to ask another question. For the OP's purpose, he can use the
    > IsNumeric() function from Microsoft.VisualBasic namespace, or attempt to
    > write one for himself with the following:
    >
    > public static bool InNumeric(string inputstr)
    > {
    > bool result = false;
    > try
    > {
    > decimal temp = Convert.ToDecimal(inputstr);
    > result = true;
    > }
    > catch
    > {
    > result = false;
    > }
    > return result;
    > }
    >
    > Of course he may also determine the result by comparing the ASCII values
    > of the string one by one for "0"-"9" and "." (possibly "e" and so on if
    > applicable)
    >
    > For the above methods, which one is recommended to do in C#?
    >
    > "Dave" <>
    > ¼¶¼g©ó¶l¥ó·s»D:...
    >> I'm trying to import Microsoft.VisualBasic to use the IsNumeric function
    >> in
    >> my C# code but all I see is:
    >>
    >> Microsoft.VisualBasic.VBCodeProvider while using Intellisense...
    >>
    >> From samples shouldn't I see Microsoft.VisualBasic.Information.IsNumeric?
    >>
    >> Thanks

    >
    >
    Kevin Spencer, Jan 16, 2006
    #5
  6. The code swallows exceptions unecessarily.

    if (value == null || value == DBNull.Value)
    {
    return false;
    }
    try
    {
    Decimal.Parse(value.ToString());
    return true;
    }
    catch (Exception ex)
    {
    if ((ex is FormatException) || (ex is InvalidCastException) || (ex is
    OverflowException))
    {
    return false;
    }
    throw;
    }


    Karl

    --
    http://www.openmymind.net/



    "Lau Lei Cheong" <> wrote in message
    news:...
    > After you imported Microsoft.VisualBasic namespace, when you type
    > "IsNumeric(", does the Intellisense show up?
    >
    > And I wish to ask another question. For the OP's purpose, he can use the
    > IsNumeric() function from Microsoft.VisualBasic namespace, or attempt to
    > write one for himself with the following:
    >
    > public static bool InNumeric(string inputstr)
    > {
    > bool result = false;
    > try
    > {
    > decimal temp = Convert.ToDecimal(inputstr);
    > result = true;
    > }
    > catch
    > {
    > result = false;
    > }
    > return result;
    > }
    >
    > Of course he may also determine the result by comparing the ASCII values
    > of the string one by one for "0"-"9" and "." (possibly "e" and so on if
    > applicable)
    >
    > For the above methods, which one is recommended to do in C#?
    >
    > "Dave" <>
    > ¼¶¼g©ó¶l¥ó·s»D:...
    >> I'm trying to import Microsoft.VisualBasic to use the IsNumeric function
    >> in
    >> my C# code but all I see is:
    >>
    >> Microsoft.VisualBasic.VBCodeProvider while using Intellisense...
    >>
    >> From samples shouldn't I see Microsoft.VisualBasic.Information.IsNumeric?
    >>
    >> Thanks

    >
    >
    Karl Seguin [MVP], Jan 16, 2006
    #6
  7. Actually, there is an overload of TryParse that can be used to do the same
    type-checking and so on. I didn't post it for simplicity's sake, but it's
    good to mention it.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    "Peter Bromberg [C# MVP]" <> wrote in message
    news:...
    > The IsNumeric method from Microsoft.VisualBasic is not that simple. It
    > does
    > an incredible amount of complex type-checking with culture, Iconvertible,
    > etc. under the hood.
    >
    > However, if you want a C# "Simplistic" version, I'd suggest using
    > double.TryParse, which does not throw an exception.
    > Peter
    >
    >
    > --
    > Co-founder, Eggheadcafe.com developer portal:
    > http://www.eggheadcafe.com
    > UnBlog:
    > http://petesbloggerama.blogspot.com
    >
    >
    >
    >
    > "Lau Lei Cheong" wrote:
    >
    >> After you imported Microsoft.VisualBasic namespace, when you type
    >> "IsNumeric(", does the Intellisense show up?
    >>
    >> And I wish to ask another question. For the OP's purpose, he can use the
    >> IsNumeric() function from Microsoft.VisualBasic namespace, or attempt to
    >> write one for himself with the following:
    >>
    >> public static bool InNumeric(string inputstr)
    >> {
    >> bool result = false;
    >> try
    >> {
    >> decimal temp = Convert.ToDecimal(inputstr);
    >> result = true;
    >> }
    >> catch
    >> {
    >> result = false;
    >> }
    >> return result;
    >> }
    >>
    >> Of course he may also determine the result by comparing the ASCII values
    >> of
    >> the string one by one for "0"-"9" and "." (possibly "e" and so on if
    >> applicable)
    >>
    >> For the above methods, which one is recommended to do in C#?
    >>
    >> "Dave" <>
    >> ¼¶¼g©ó¶l¥ó·s»D:...
    >> > I'm trying to import Microsoft.VisualBasic to use the IsNumeric
    >> > function
    >> > in
    >> > my C# code but all I see is:
    >> >
    >> > Microsoft.VisualBasic.VBCodeProvider while using Intellisense...
    >> >
    >> > From samples shouldn't I see
    >> > Microsoft.VisualBasic.Information.IsNumeric?
    >> >
    >> > Thanks

    >>
    >>
    >>
    Kevin Spencer, Jan 16, 2006
    #7
  8. You should never code around exception handling into your core logic.
    Exception handling should be handling true exceptions, not the things you
    expect. (It's also very inefficient to handle exceptions within your core
    logic).
    --
    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter & VB to C++ converter
    Instant J#: VB to J# converter



    "Lau Lei Cheong" wrote:

    > After you imported Microsoft.VisualBasic namespace, when you type
    > "IsNumeric(", does the Intellisense show up?
    >
    > And I wish to ask another question. For the OP's purpose, he can use the
    > IsNumeric() function from Microsoft.VisualBasic namespace, or attempt to
    > write one for himself with the following:
    >
    > public static bool InNumeric(string inputstr)
    > {
    > bool result = false;
    > try
    > {
    > decimal temp = Convert.ToDecimal(inputstr);
    > result = true;
    > }
    > catch
    > {
    > result = false;
    > }
    > return result;
    > }
    >
    > Of course he may also determine the result by comparing the ASCII values of
    > the string one by one for "0"-"9" and "." (possibly "e" and so on if
    > applicable)
    >
    > For the above methods, which one is recommended to do in C#?
    >
    > "Dave" <> ¼¶¼g©ó¶l¥ó·s»D:...
    > > I'm trying to import Microsoft.VisualBasic to use the IsNumeric function
    > > in
    > > my C# code but all I see is:
    > >
    > > Microsoft.VisualBasic.VBCodeProvider while using Intellisense...
    > >
    > > From samples shouldn't I see Microsoft.VisualBasic.Information.IsNumeric?
    > >
    > > Thanks

    >
    >
    >
    =?Utf-8?B?RGF2aWQgQW50b24=?=, Jan 16, 2006
    #8
  9. I see.

    But... for the purpose of assignment to a variable, I think checking to see
    if it can be successfully assigned to a variable of that variable type is
    good enough. Isn't it?

    "Peter Bromberg [C# MVP]" <> ¼¶¼g©ó¶l¥ó·s»D:...
    > The IsNumeric method from Microsoft.VisualBasic is not that simple. It
    > does
    > an incredible amount of complex type-checking with culture, Iconvertible,
    > etc. under the hood.
    >
    > However, if you want a C# "Simplistic" version, I'd suggest using
    > double.TryParse, which does not throw an exception.
    > Peter
    >
    >
    > --
    > Co-founder, Eggheadcafe.com developer portal:
    > http://www.eggheadcafe.com
    > UnBlog:
    > http://petesbloggerama.blogspot.com
    >
    >
    >
    >
    > "Lau Lei Cheong" wrote:
    >
    >> After you imported Microsoft.VisualBasic namespace, when you type
    >> "IsNumeric(", does the Intellisense show up?
    >>
    >> And I wish to ask another question. For the OP's purpose, he can use the
    >> IsNumeric() function from Microsoft.VisualBasic namespace, or attempt to
    >> write one for himself with the following:
    >>
    >> public static bool InNumeric(string inputstr)
    >> {
    >> bool result = false;
    >> try
    >> {
    >> decimal temp = Convert.ToDecimal(inputstr);
    >> result = true;
    >> }
    >> catch
    >> {
    >> result = false;
    >> }
    >> return result;
    >> }
    >>
    >> Of course he may also determine the result by comparing the ASCII values
    >> of
    >> the string one by one for "0"-"9" and "." (possibly "e" and so on if
    >> applicable)
    >>
    >> For the above methods, which one is recommended to do in C#?
    >>
    >> "Dave" <> ???gco?l¢Do¡Ps?D:...
    >>
    >> > I'm trying to import Microsoft.VisualBasic to use the IsNumeric
    >> > function
    >> > in
    >> > my C# code but all I see is:
    >> >
    >> > Microsoft.VisualBasic.VBCodeProvider while using Intellisense...
    >> >
    >> > From samples shouldn't I see
    >> > Microsoft.VisualBasic.Information.IsNumeric?
    >> >
    >> > Thanks

    >>
    >>
    >>
    Lau Lei Cheong, Jan 17, 2006
    #9
    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. darrel
    Replies:
    2
    Views:
    398
    Mark Fitzpatrick
    Aug 19, 2004
  2. darrel
    Replies:
    0
    Views:
    389
    darrel
    Aug 19, 2004
  3. David P. Donahue

    Latest Microsoft.VisualBasic.dll?

    David P. Donahue, Oct 4, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    1,857
    David P. Donahue
    Oct 5, 2004
  4. Marco Viana
    Replies:
    0
    Views:
    633
    Marco Viana
    Apr 22, 2005
  5. =?Utf-8?B?VGltIE1leWVy?=

    Unable to load microsoft.visualbasic.dll

    =?Utf-8?B?VGltIE1leWVy?=, Dec 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    655
    =?Utf-8?B?VGltIE1leWVy?=
    Dec 20, 2005
Loading...

Share This Page