473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ItemI nserting_Valida te(object sender,
FormViewInsertE ventArgs e)

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

I tried the following, but it seems awfully cluncky. Thanks, sck10
protected void fvFunding_ItemI nserting_Valida te(object sender,
FormViewInsertE ventArgs e)

// verify the textbox contains an integer
if (e.Values["Funding"].ToString() != "")
{
try {int i = Convert.ToInt32 (e.Values["Funding"].ToString(), 10);}
catch (FormatExceptio n i)
{
e.Cancel = true;
this.lblMessage Text.Text += "Annual Rev: Numbers only.<br />";
}
} // end if
}
Sep 2 '06
12 3064
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(objec t Expression)
{
IConvertible convertible1 = Expression as IConvertible;
if (convertible1 != null)
{
switch (convertible1.G etTypeCode())
{
case TypeCode.Boolea n:
return true;

case TypeCode.Char:
case TypeCode.String :
{
double num1;
string text1 = convertible1.To String(null);
try
{
long num2;
if (Utils.IsHexOrO ctValue(text1, ref num2))
{
return true;
}
}
catch (FormatExceptio n)
{
return false;
}
return Conversions.Try ParseDouble(tex t1, 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.Decima l:
return true;
}
}
return false;
}
Example #2
========
internal static bool IsNumeric(Stora geType type)
{
if (!ExpressionNod e.IsFloat(type) )
{
return ExpressionNode. IsInteger(type) ;
}
return true;
}

Example #3
========
public static bool IsNumeric(objec t 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.Ge tTypeCode();
if ((code1 != TypeCode.String ) && (code1 != TypeCode.Char))
{
return Information.IsO ldNumericTypeCo de(code1);
}
string text1 = convertible1.To String(null);
try
{
long num2;
if (Utils.IsHexOrO ctValue(text1, ref num2))
{
return true;
}
}
catch (StackOverflowE xception exception1)
{
throw exception1;
}
catch (OutOfMemoryExc eption exception2)
{
throw exception2;
}
catch (ThreadAbortExc eption exception3)
{
throw exception3;
}
catch (Exception)
{
return false;
}
return DoubleType.TryP arse(text1, ref num1);
}


"Walter Wang [MSFT]" <wa****@online. microsoft.comwr ote in message
news:Yj******** ******@TK2MSFTN GXA01.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.WriteLi ne("s is numeric")
End If
s = "2006/9/1"
If IsDate(s) Then
Console.WriteLi ne("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.IsNu meric(text1))
{
Console.WriteLi ne("s is numeric");
}
text1 = "2006/9/1";
if (Information.Is Date(text1))
{
Console.WriteLi ne("s is date");
}
}

3) Click on the "IsNumeric" function to navigate to its source. You will
learn that it's located in
Microsoft.Visua lBasic.Compiler Services.Versio ned
as a static method, so you can reference Microsoft.Visua lBasic.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.Exp ressionNode 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.Visua lBasic.Informat ion; while #1 is from
Microsoft.Visua lBasic.Compiler Services.Versio ned. 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.Visua lBasic.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.IsHexOrOc tValue. 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.comwr ote in message
news:a8******** *****@TK2MSFTNG XA01.phx.gbl...
Hi sck10,

Good digging!

The #2 is from System.Data.Exp ressionNode 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.Visua lBasic.Informat ion; while #1 is from
Microsoft.Visua lBasic.Compiler Services.Versio ned. 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.Visua lBasic.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.IsHexOrOc tValue. 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2324
by: eje | last post by:
IsNumeric(value) should return false if value "can not be successfully converted to a Double." Instead I get the following error message: "Input string was not in a correct format." I use the following function in a validating class to use when needed. (Value = H880118A gave the error (like other 'unconvertible' strings)) Public Function Numeric(ByVal value) As Boolean
4
13276
by: Eugene Anthony | last post by:
I have received the following feedback for the two functions bellow: "The ISNUMERIC test is WORTHLESS for checking for an INT value, because ISNUMERIC will happily accept DOUBLE values, such as 89.11998811777 and other values that are simply *NOT* INT values." <% function isZip(input)
14
40147
by: Kenny | last post by:
Hello, I would like to know if the function IsNumeric requires a header like #include <iostream> to be functionnal thanks ken
8
2614
by: John Bowman | last post by:
Hello, Does anyone have a good/reliable approach to implementing an IsNumeric() method that accepts a string that may represent a numerical value (eg. such as some text retrieved from an XML file)? Thus if you pass it "1" or "5.12345" it returns true, but "1b3q" it returns false? I know VB has this sort of function, but was wondering how to implement something similar in C#. I suppose I could use the Convert methods and catch any...
3
1765
by: martin | last post by:
Hi, is there a dotnet function (other than the old isnumeric from VB) to check whether an object is numeric or not. also I notice that all the old vb functions such as split / isnumeric / ubound etc are still availible by default in any VB.net web application. is this because the vb library is being imported?? would it be best to totally remove this library (assuming that it is being imported - which I can't see in my references) so...
7
2385
by: Nathan Truhan | last post by:
All, I think I may have uncovered a bug in the IsNumeric function, or at least a misunderstanding on functionality. I am writing a Schedule Of Classes Application for our campus and have a section that lists the Times, Buildings and rooms for courses. In this section I have a function called PadCell that takes3 parameters, one the value, one a padd count and one a a boolean input called StripNumeric, that if true, checks if the value...
8
2590
by: moondaddy | last post by:
What's the .net framework equivalent of the vb function isnumeric? If there isn't one, how can I test a string variable to see if its a number or not? I don't want to use isnumeric if possible -- moondaddy@noemail.noemail
12
1666
by: Paul | last post by:
Hi, I am trying to check a string to see if it's first 3 characters are numeric and if they are, to replace those 3 characters with something else. I've tried this but nothing happens... newname = Replace(newname, VB.Left(newname, 3) = "101-", "101. ")
17
2302
by: MLH | last post by:
I have tested the following in immed window: ?isnumeric(1) True ?isnumeric(1.) True ?isnumeric(1.2) True ?isnumeric(1.2.2)
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.