473,411 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

isnumeric in c sharp

I have a pocket pc project in c#.

From a textbox i have to verify if the input is numeric. I have found
in the msdn a sample like this: textbox1.numeric = true / false. But
this do not work in my environment.

If I convert the textbox content explicit to a numeric value, and is
is not numeric, the program ran into the error handler (try catch).
This is not what I want!

For example: 2 textbox, 1 button
If the user click the button, the program should proof if the content
of the textboxes is numeric, if not, it should use a standard value
and then go on.

A nice method like 'isnumeric' would be glad.

Thanks for all assistance.
Nov 15 '05 #1
10 29253
Hi,

You can attempt to feed the textbox's Text property value to the Parse
method of the Int32 type, and catch the FormatException, OverflowException
and optionally ArgumentNullException (this one is unlikely to be raised).
If any of the exceptions has been thrown, the textbox's text is not a valid
integer.

This approach works the same way for Double, Decimal or any other type
supporting the Parse method.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"crowl" <cr***@gmx.de> wrote in message
news:e1**************************@posting.google.c om...
I have a pocket pc project in c#.

From a textbox i have to verify if the input is numeric. I have found
in the msdn a sample like this: textbox1.numeric = true / false. But
this do not work in my environment.

If I convert the textbox content explicit to a numeric value, and is
is not numeric, the program ran into the error handler (try catch).
This is not what I want!

For example: 2 textbox, 1 button
If the user click the button, the program should proof if the content
of the textboxes is numeric, if not, it should use a standard value
and then go on.

A nice method like 'isnumeric' would be glad.

Thanks for all assistance.


Nov 15 '05 #2
Is there a reason you can't use a try / catch block with System.Convert

such as

void Button_Click(object sender, eventargs e

// verify the textbox contains an intege
tr

int i = System.Convert.ToInt32(TextBox1.Text, 10)
// if you get here then the i contains the integer value of TextBox1.Tex

catch (System.FormatException e

// the conversion failed so use the default value
i = defaultValue

// Continue processing
Jackson Davis [msft
-
This posting is provided "AS IS" with no warranties, and confers no right
Samples are subject to the terms specified a
http://www.microsoft.com/info/cpyright.ht

Nov 15 '05 #3
Hello Jackson,

I think catching FormatException only would be an unreliable solution.
Imagine the user has typed something like "9999999999999999999999999999" to
the textbox. In this case, I suppose an OverflowException instance will be
thrown upon calling the ToInt32 method, not a FormatException.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Jackson Davis [MSFT]" <an*******@discussions.microsoft.com> wrote in
message news:81**********************************@microsof t.com...
Is there a reason you can't use a try / catch block with System.Convert?

such as:

void Button_Click(object sender, eventargs e)
{
// verify the textbox contains an integer
try
{
int i = System.Convert.ToInt32(TextBox1.Text, 10);
// if you get here then the i contains the integer value of TextBox1.Text }
catch (System.FormatException e)
{
// the conversion failed so use the default value.
i = defaultValue;
}
// Continue processing.
}

Jackson Davis [msft]
--
This posting is provided "AS IS" with no warranties, and confers no rights
Samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


Nov 15 '05 #4
crowl wrote:
I have a pocket pc project in c#.

From a textbox i have to verify if the input is numeric. I have found
in the msdn a sample like this: textbox1.numeric = true / false. But
this do not work in my environment.

If I convert the textbox content explicit to a numeric value, and is
is not numeric, the program ran into the error handler (try catch).
This is not what I want!

For example: 2 textbox, 1 button
If the user click the button, the program should proof if the content
of the textboxes is numeric, if not, it should use a standard value
and then go on.

A nice method like 'isnumeric' would be glad.


You can use VB.NET's IsNumeric method. Add a reference to
Microsoft.VisualBasic.dll, then call the static method:

Microsoft.VisualBasic.Information.IsNumeric( object)

--
mikeb
Nov 15 '05 #5
Here is my adaptation to the IL of the VB "IsNumeric" method. Of course,
this is not as exhausting. There is a lot of IL I can't make sense of and a
lot of Locale stuff I didn't even attempt. Assuming you using ASCII
characters in the string and all others, numerics, this should work.
---------------------------------------------------

using System;

namespace CSharpInformation
{

public class Information
{
public static bool IsNumeric(object expression) {

IConvertible convertible = null;
TypeCode typecode;
string text;
double num;
bool result = false;
char chr;

if ((expression as IConvertible) != null) {
convertible = (IConvertible)expression;
}

if (convertible == null) {
if ((expression as char[]) != null) {
expression = new string((char[])expression);
}
else {
return false;
}
}

typecode = convertible.GetTypeCode();

if (typecode == TypeCode.String || typecode == TypeCode.Char) {
text = convertible.ToString(null);

try {
for (int i=0; i<text.Length; i++) {
chr = Convert.ToChar(text.Substring(i, 1));

if (char.IsNumber(chr)) {
result = true;
}
else if ((int)chr >= 'a' && (int)chr <= 'f') {
result = true;
}
else if ((int)chr >= 'A' && (int)chr <= 'F') {
result = true;
}
else {
result = false;
}

if (result == false) {
break;
}
}
}
catch(Exception) {
return false;
}

if (result == false) {
result = double.TryParse(text,
System.Globalization.NumberStyles.Any, null, out num);
}
}

if (result == false) {
result = IsNumericTypeCode(typecode);
}

return result;
}

internal static bool IsNumericTypeCode(TypeCode typeCode) {
bool result = false;

switch (typeCode) {
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
result = true;
break;

default:
result = false;
break;
}

return result;
}
}
}
Nov 15 '05 #6

public static bool IsNumeric(object value)
{
try
{
double d = System.Double.Parse(value.ToString(),
System.Globalization.NumberStyles.Any);
return true;
}
catch (FormatException)
{
return false;
}
}


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #7
Here is some code that doesn't rely on error trapping:

public static bool IsNumeric(string stringToTest)

{

double newVal;

return double.TryParse(stringToTest, NumberStyles.Any,
NumberFormatInfo.InvariantInfo, out newVal);

}

"crowl" <cr***@gmx.de> wrote in message
news:e1**************************@posting.google.c om...
I have a pocket pc project in c#.

From a textbox i have to verify if the input is numeric. I have found
in the msdn a sample like this: textbox1.numeric = true / false. But
this do not work in my environment.

If I convert the textbox content explicit to a numeric value, and is
is not numeric, the program ran into the error handler (try catch).
This is not what I want!

For example: 2 textbox, 1 button
If the user click the button, the program should proof if the content
of the textboxes is numeric, if not, it should use a standard value
and then go on.

A nice method like 'isnumeric' would be glad.

Thanks for all assistance.

Nov 15 '05 #8
This won't detect Hexadecimal values.
Thanks,
Shawn

"Dave Lech" <da*******@tyson.com> wrote in message
news:e4**************@TK2MSFTNGP11.phx.gbl...
Here is some code that doesn't rely on error trapping:

public static bool IsNumeric(string stringToTest)

{

double newVal;

return double.TryParse(stringToTest, NumberStyles.Any,
NumberFormatInfo.InvariantInfo, out newVal);

}

"crowl" <cr***@gmx.de> wrote in message
news:e1**************************@posting.google.c om...
I have a pocket pc project in c#.

From a textbox i have to verify if the input is numeric. I have found
in the msdn a sample like this: textbox1.numeric = true / false. But
this do not work in my environment.

If I convert the textbox content explicit to a numeric value, and is
is not numeric, the program ran into the error handler (try catch).
This is not what I want!

For example: 2 textbox, 1 button
If the user click the button, the program should proof if the content
of the textboxes is numeric, if not, it should use a standard value
and then go on.

A nice method like 'isnumeric' would be glad.

Thanks for all assistance.


Nov 15 '05 #9
Thank you all for the nice ideas!

I think a own methode fits to my need.

Thanks again.

"Shawn B." <le****@html.com> wrote in message news:<#a**************@TK2MSFTNGP09.phx.gbl>...
This won't detect Hexadecimal values.
Thanks,
Shawn

"Dave Lech" <da*******@tyson.com> wrote in message
news:e4**************@TK2MSFTNGP11.phx.gbl...
Here is some code that doesn't rely on error trapping:

public static bool IsNumeric(string stringToTest)

{

double newVal;

return double.TryParse(stringToTest, NumberStyles.Any,
NumberFormatInfo.InvariantInfo, out newVal);

}

"crowl" <cr***@gmx.de> wrote in message
news:e1**************************@posting.google.c om...
I have a pocket pc project in c#.

From a textbox i have to verify if the input is numeric. I have found
in the msdn a sample like this: textbox1.numeric = true / false. But
this do not work in my environment.

If I convert the textbox content explicit to a numeric value, and is
is not numeric, the program ran into the error handler (try catch).
This is not what I want!

For example: 2 textbox, 1 button
If the user click the button, the program should proof if the content
of the textboxes is numeric, if not, it should use a standard value
and then go on.

A nice method like 'isnumeric' would be glad.

Thanks for all assistance.


Nov 15 '05 #10
At the least, I have to admit that the double.TryParse works well and is a 1
liner. Looking at the VB IsNumeric clone I posted above, it is much
simpler.

Thanks,
Shawn

"crowl" <cr***@gmx.de> wrote in message
news:e1**************************@posting.google.c om...
Thank you all for the nice ideas!

I think a own methode fits to my need.

Thanks again.

"Shawn B." <le****@html.com> wrote in message

news:<#a**************@TK2MSFTNGP09.phx.gbl>...
This won't detect Hexadecimal values.
Thanks,
Shawn

"Dave Lech" <da*******@tyson.com> wrote in message
news:e4**************@TK2MSFTNGP11.phx.gbl...
Here is some code that doesn't rely on error trapping:

public static bool IsNumeric(string stringToTest)

{

double newVal;

return double.TryParse(stringToTest, NumberStyles.Any,
NumberFormatInfo.InvariantInfo, out newVal);

}

"crowl" <cr***@gmx.de> wrote in message
news:e1**************************@posting.google.c om...
> I have a pocket pc project in c#.
>
> From a textbox i have to verify if the input is numeric. I have found > in the msdn a sample like this: textbox1.numeric = true / false. But
> this do not work in my environment.
>
> If I convert the textbox content explicit to a numeric value, and is
> is not numeric, the program ran into the error handler (try catch).
> This is not what I want!
>
> For example: 2 textbox, 1 button
> If the user click the button, the program should proof if the content > of the textboxes is numeric, if not, it should use a standard value
> and then go on.
>
> A nice method like 'isnumeric' would be glad.
>
> Thanks for all assistance.

Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
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...
4
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...
14
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
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...
7
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...
8
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 ...
12
by: sck10 | last post by:
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...
12
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... ...
17
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.