468,785 Members | 1,536 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

IsNumeric

Hello,

I am trying to determine if a value is NOT numeric in C#. How do you test
for "Not IsNumeric"?

protected void fvFunding_ItemInserting_Validate(object sender,
FormViewInsertEventArgs e)

if (e.NewValues["Funding"] != "" && Not IsNumeric(e.NewValues["Funding"]))
{
e.Cancel = true;
this.MessageText.Text += "Annual Rev: Numbers only.<br />";
}
}

I tried the following, but it seems awfully cluncky. Thanks, sck10
protected void fvFunding_ItemInserting_Validate(object sender,
FormViewInsertEventArgs e)

// verify the textbox contains an integer
if (e.Values["Funding"].ToString() != "")
{
try {int i = Convert.ToInt32(e.Values["Funding"].ToString(), 10);}
catch (FormatException i)
{
e.Cancel = true;
this.lblMessageText.Text += "Annual Rev: Numbers only.<br />";
}
} // end if
}
Sep 2 '06 #1
12 2716
These might be interesting

http://www.hanselman.com/blog/Explor...mericForC.aspx
http://weblogs.asp.net/justin_rogers...29/100982.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"sck10" <sc***@online.nospamwrote in message
news:eh**************@TK2MSFTNGP02.phx.gbl...
Hello,

I am trying to determine if a value is NOT numeric in C#. How do you test
for "Not IsNumeric"?

protected void fvFunding_ItemInserting_Validate(object sender,
FormViewInsertEventArgs e)

if (e.NewValues["Funding"] != "" && Not
IsNumeric(e.NewValues["Funding"]))
{
e.Cancel = true;
this.MessageText.Text += "Annual Rev: Numbers only.<br />";
}
}

I tried the following, but it seems awfully cluncky. Thanks, sck10
protected void fvFunding_ItemInserting_Validate(object sender,
FormViewInsertEventArgs e)

// verify the textbox contains an integer
if (e.Values["Funding"].ToString() != "")
{
try {int i = Convert.ToInt32(e.Values["Funding"].ToString(), 10);}
catch (FormatException i)
{
e.Cancel = true;
this.lblMessageText.Text += "Annual Rev: Numbers only.<br />";
}
} // end if
}


Sep 2 '06 #2
Double.TryParse is a clean way to go. Here's an example where I've wrapped
it in a C# method:
http://www.tangiblesoftwaresolutions...0IsNumeric.htm
--
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 determine if a value is NOT numeric in C#. How do you test
for "Not IsNumeric"?

protected void fvFunding_ItemInserting_Validate(object sender,
FormViewInsertEventArgs e)

if (e.NewValues["Funding"] != "" && Not IsNumeric(e.NewValues["Funding"]))
{
e.Cancel = true;
this.MessageText.Text += "Annual Rev: Numbers only.<br />";
}
}

I tried the following, but it seems awfully cluncky. Thanks, sck10
protected void fvFunding_ItemInserting_Validate(object sender,
FormViewInsertEventArgs e)

// verify the textbox contains an integer
if (e.Values["Funding"].ToString() != "")
{
try {int i = Convert.ToInt32(e.Values["Funding"].ToString(), 10);}
catch (FormatException i)
{
e.Cancel = true;
this.lblMessageText.Text += "Annual Rev: Numbers only.<br />";
}
} // end if
}
Sep 2 '06 #3
Thanks Teemu...
"Teemu Keiski" <jo****@aspalliance.comwrote in message
news:eQ**************@TK2MSFTNGP06.phx.gbl...
These might be interesting

http://www.hanselman.com/blog/Explor...mericForC.aspx
http://weblogs.asp.net/justin_rogers...29/100982.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"sck10" <sc***@online.nospamwrote in message
news:eh**************@TK2MSFTNGP02.phx.gbl...
>Hello,

I am trying to determine if a value is NOT numeric in C#. How do you
test for "Not IsNumeric"?

protected void fvFunding_ItemInserting_Validate(object sender,
FormViewInsertEventArgs e)

if (e.NewValues["Funding"] != "" && Not
IsNumeric(e.NewValues["Funding"]))
{
e.Cancel = true;
this.MessageText.Text += "Annual Rev: Numbers only.<br />";
}
}

I tried the following, but it seems awfully cluncky. Thanks, sck10
protected void fvFunding_ItemInserting_Validate(object sender,
FormViewInsertEventArgs e)

// verify the textbox contains an integer
if (e.Values["Funding"].ToString() != "")
{
try {int i = Convert.ToInt32(e.Values["Funding"].ToString(), 10);}
catch (FormatException i)
{
e.Cancel = true;
this.lblMessageText.Text += "Annual Rev: Numbers only.<br />";
}
} // end if
}



Sep 2 '06 #4
Thanks David, I'll give it a shot...
"David Anton" <Da********@discussions.microsoft.comwrote in message
news:E0**********************************@microsof t.com...
Double.TryParse is a clean way to go. Here's an example where I've
wrapped
it in a C# method:
http://www.tangiblesoftwaresolutions...0IsNumeric.htm
--
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 determine if a value is NOT numeric in C#. How do you
test
for "Not IsNumeric"?

protected void fvFunding_ItemInserting_Validate(object sender,
FormViewInsertEventArgs e)

if (e.NewValues["Funding"] != "" && Not
IsNumeric(e.NewValues["Funding"]))
{
e.Cancel = true;
this.MessageText.Text += "Annual Rev: Numbers only.<br />";
}
}

I tried the following, but it seems awfully cluncky. Thanks, sck10
protected void fvFunding_ItemInserting_Validate(object sender,
FormViewInsertEventArgs e)

// verify the textbox contains an integer
if (e.Values["Funding"].ToString() != "")
{
try {int i = Convert.ToInt32(e.Values["Funding"].ToString(), 10);}
catch (FormatException i)
{
e.Cancel = true;
this.lblMessageText.Text += "Annual Rev: Numbers only.<br />";
}
} // end if
}

Sep 2 '06 #5
Hi sck10,

It seems you're converting lots of VB.NET code to C# recently, right? I
recommend you first write the code in VB.NET, and then use Reflector to
view its disassembled code in C#. Sometimes, VB.NET is using functions from
Microsoft.VisualBasic.dll, if you do not want to reference this assembly,
you can also check its disassembled code in C# and try to incorporate it
into your code.

Regarding this question, please feel free to post here if you need anything
else.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 4 '06 #6
Yes, Walter, you are right about the conversion. I am trying to learn c#
and the best way for me is by converting my vb.net applications to c#.

Anyway, thanks again...
"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:vZ**************@TK2MSFTNGXA01.phx.gbl...
Hi sck10,

It seems you're converting lots of VB.NET code to C# recently, right? I
recommend you first write the code in VB.NET, and then use Reflector to
view its disassembled code in C#. Sometimes, VB.NET is using functions
from
Microsoft.VisualBasic.dll, if you do not want to reference this assembly,
you can also check its disassembled code in C# and try to incorporate it
into your code.

Regarding this question, please feel free to post here if you need
anything
else.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Sep 4 '06 #7
Hi Walter,

I just downloaded the Reflector tool. Can you point me to where I would
find IsNumeric or IsDate so I could see how it converts to c#?

Thanks again, sck10

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:vZ**************@TK2MSFTNGXA01.phx.gbl...
Hi sck10,

It seems you're converting lots of VB.NET code to C# recently, right? I
recommend you first write the code in VB.NET, and then use Reflector to
view its disassembled code in C#. Sometimes, VB.NET is using functions
from
Microsoft.VisualBasic.dll, if you do not want to reference this assembly,
you can also check its disassembled code in C# and try to incorporate it
into your code.

Regarding this question, please feel free to post here if you need
anything
else.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Sep 4 '06 #8
Hi sck10,

Thank you for your quick reply.

Here's the steps to get the source code of IsNumeric:

1) Create a simple VB.NET console application, type in following code:

Sub Main()
Dim s As String = "1.234"
If IsNumeric(s) Then
Console.WriteLine("s is numeric")
End If
s = "2006/9/1"
If IsDate(s) Then
Console.WriteLine("s is date")
End If
End Sub

2) Build it; in Reflector, open the generated exe; find the Main function,
double click it to see its disassembled code (make sure you selected "C#"
in the toolbar combobox):

[STAThread]
public static void Main()
{
string text1 = "1.234";
if (Versioned.IsNumeric(text1))
{
Console.WriteLine("s is numeric");
}
text1 = "2006/9/1";
if (Information.IsDate(text1))
{
Console.WriteLine("s is date");
}
}

3) Click on the "IsNumeric" function to navigate to its source. You will
learn that it's located in Microsoft.VisualBasic.CompilerServices.Versioned
as a static method, so you can reference Microsoft.VisualBasic.dll in your
C# project and use this method directly; or you can write your version of
IsNumeric use the disassembled code as reference.

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 4 '06 #9
Hi sck10,

Another note, if you checked Reflector's "View/Options/Show PDB Symbols"
and you're disassembling an assembly with full .pdb file, you will see the
correct variable name:

[STAThread]
public static void Main()
{
string s = "1.234";
if (Versioned.IsNumeric(s))
{
Console.WriteLine("s is numeric");
}
s = "2006/9/1";
if (Information.IsDate(s))
{
Console.WriteLine("s is date");
}
}

Previously I'm not checking that option, so the variable "s" has a generic
name "text1".
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 4 '06 #10
Thanks Walter. I was rummaging around the Reflector tool and came these
three definitions. The first is from the definition from "Main" that you
provided. Can you help me with the last two? So my understanding is that I
can use example #1 for my needs?

Example #1
========
public static bool IsNumeric(object Expression)
{
IConvertible convertible1 = Expression as IConvertible;
if (convertible1 != null)
{
switch (convertible1.GetTypeCode())
{
case TypeCode.Boolean:
return true;

case TypeCode.Char:
case TypeCode.String:
{
double num1;
string text1 = convertible1.ToString(null);
try
{
long num2;
if (Utils.IsHexOrOctValue(text1, ref num2))
{
return true;
}
}
catch (FormatException)
{
return false;
}
return Conversions.TryParseDouble(text1, ref num1);
}
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
}
}
return false;
}
Example #2
========
internal static bool IsNumeric(StorageType type)
{
if (!ExpressionNode.IsFloat(type))
{
return ExpressionNode.IsInteger(type);
}
return true;
}

Example #3
========
public static bool IsNumeric(object Expression)
{
double num1;
IConvertible convertible1 = Expression as IConvertible;
if (convertible1 == null)
{
char[] chArray1 = Expression as char[];
if (chArray1 != null)
{
Expression = new string(chArray1);
}
else
{
return false;
}
}
TypeCode code1 = convertible1.GetTypeCode();
if ((code1 != TypeCode.String) && (code1 != TypeCode.Char))
{
return Information.IsOldNumericTypeCode(code1);
}
string text1 = convertible1.ToString(null);
try
{
long num2;
if (Utils.IsHexOrOctValue(text1, ref num2))
{
return true;
}
}
catch (StackOverflowException exception1)
{
throw exception1;
}
catch (OutOfMemoryException exception2)
{
throw exception2;
}
catch (ThreadAbortException exception3)
{
throw exception3;
}
catch (Exception)
{
return false;
}
return DoubleType.TryParse(text1, ref num1);
}


"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:Yj**************@TK2MSFTNGXA01.phx.gbl...
Hi sck10,

Thank you for your quick reply.

Here's the steps to get the source code of IsNumeric:

1) Create a simple VB.NET console application, type in following code:

Sub Main()
Dim s As String = "1.234"
If IsNumeric(s) Then
Console.WriteLine("s is numeric")
End If
s = "2006/9/1"
If IsDate(s) Then
Console.WriteLine("s is date")
End If
End Sub

2) Build it; in Reflector, open the generated exe; find the Main function,
double click it to see its disassembled code (make sure you selected "C#"
in the toolbar combobox):

[STAThread]
public static void Main()
{
string text1 = "1.234";
if (Versioned.IsNumeric(text1))
{
Console.WriteLine("s is numeric");
}
text1 = "2006/9/1";
if (Information.IsDate(text1))
{
Console.WriteLine("s is date");
}
}

3) Click on the "IsNumeric" function to navigate to its source. You will
learn that it's located in
Microsoft.VisualBasic.CompilerServices.Versioned
as a static method, so you can reference Microsoft.VisualBasic.dll in your
C# project and use this method directly; or you can write your version of
IsNumeric use the disassembled code as reference.

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Sep 7 '06 #11
Hi sck10,

Good digging!

The #2 is from System.Data.ExpressionNode which I think is used to
determine whether or not a database data type is numeric type. So I think
it's not applicable here for your requirement.

#3 is from class Microsoft.VisualBasic.Information; while #1 is from
Microsoft.VisualBasic.CompilerServices.Versioned. If you look at the logic
of both methods, they are basically similar here.

Choose which one is not important here, I think learning the implementation
detail is the key. Just like other community members contributed, this can
be done using different ways depending on your requirement. For example,
the implementation in Microsoft.VisualBasic.dll can treat "100h" as a
numeric value since it's a notation of hex value in VB; if you need to
handle "0x100" like in C#, then you need to change the logic too;
otherwise, you will not correctly recogonize the value.

If you need to copy the #1 or #3 implemenation to your c# code, then you
need to also copy the other methods referenced by them such as
Utils.IsHexOrOctValue. Again, my initial thought of introducing you to the
Reflector tool to learn the disassembled code is because I think learning
the code can help you write your own version of IsNumeric in C#.

Based on your requirement, a Double.TryParse maybe enough, though.

I hope you can learn something from this post, at least know how to use the
Reflector tool. I believe you will find it's very useful in your journey of
working with .NET.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 7 '06 #12
Walter,

Thank you for the explanation. Yes, this has been very helpful in my
transition from VB to C#.

Anyway, all your help is greatly appreciated...

sck10
"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:a8*************@TK2MSFTNGXA01.phx.gbl...
Hi sck10,

Good digging!

The #2 is from System.Data.ExpressionNode which I think is used to
determine whether or not a database data type is numeric type. So I think
it's not applicable here for your requirement.

#3 is from class Microsoft.VisualBasic.Information; while #1 is from
Microsoft.VisualBasic.CompilerServices.Versioned. If you look at the logic
of both methods, they are basically similar here.

Choose which one is not important here, I think learning the
implementation
detail is the key. Just like other community members contributed, this can
be done using different ways depending on your requirement. For example,
the implementation in Microsoft.VisualBasic.dll can treat "100h" as a
numeric value since it's a notation of hex value in VB; if you need to
handle "0x100" like in C#, then you need to change the logic too;
otherwise, you will not correctly recogonize the value.

If you need to copy the #1 or #3 implemenation to your c# code, then you
need to also copy the other methods referenced by them such as
Utils.IsHexOrOctValue. Again, my initial thought of introducing you to the
Reflector tool to learn the disassembled code is because I think learning
the code can help you write your own version of IsNumeric in C#.

Based on your requirement, a Double.TryParse maybe enough, though.

I hope you can learn something from this post, at least know how to use
the
Reflector tool. I believe you will find it's very useful in your journey
of
working with .NET.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Sep 7 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by eje | last post: by
4 posts views Thread by Eugene Anthony | last post: by
14 posts views Thread by Kenny | last post: by
8 posts views Thread by John Bowman | last post: by
3 posts views Thread by martin | last post: by
7 posts views Thread by Nathan Truhan | last post: by
8 posts views Thread by moondaddy | last post: by
12 posts views Thread by Paul | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.