Import Microsoft.VisualBasic for IsNumeric?

G

Guest

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
 
G

Guest

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
 
L

Lau Lei Cheong

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#?
 
G

Guest

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
 
K

Kevin Spencer

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

Karl Seguin [MVP]

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
 
K

Kevin Spencer

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

Guest

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
 
L

Lau Lei Cheong

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top