Solved

IsNumber method in C#?

Posted on 2001-08-22
9
3,750 Views
Last Modified: 2008-02-01
Given:
string str = textbox1.Text

Can you determine if the string is numeric in C# by doing something along the lines of:

IsNumber(str)
{
   try
   {
      int.Parse(str)
      return true;
   }
   catch
   {
      (System.FormatException)
      return false;
   }
}
I kind of made that up as I went along.
I have also been toying with the notion of checking the Ascii value of the string, but did not get very far with that idea.
A little help here would be appreciated.

bruwmac
0
Comment
Question by:bruwmac
  • 4
  • 4
9 Comments
 
LVL 6

Expert Comment

by:MichaelS
ID: 6416622
Did you checked functions
isalpha() and isdigit()?

I am not really familiar with C# but hope you can use it.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6417231
in C# it is IsDigit and IsLetter they both can work n a char or an ofsset into a supplied string - there is also IsNumber which can be used for decimal or hex - there are many other Is... functions
0
 

Author Comment

by:bruwmac
ID: 6417594
I checked for IsDigit, IsLetter, and IsNumber in the help index. With the Visual C# filter, they  aren't indexed, though they show up under Visual C# and related filter. However, I can't seem to get them to go.
ShaunWilde, if you recall my sort method you helped me with yesterday, I am trying to check the textbox input for numeric input.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6417968
are you looking for a number or just a single digit

eg a string of 23764523764 being converted to a number

or just lookig at an element eg [0] = 2 , [3] = 6
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:bruwmac
ID: 6418119
The Sort method called within my method sorts the strings entered into the textbox based on the first character of the string. If the string happens to be a number, 2006 will erroneously be sorted before 378, based on the the first characters, 2 being smaller than 3. If I can test the first character of the string to determine if it is numeric, I can fire off a messagebox saying "Please do not enter numbers."
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6418225
ah - You may have to handle an event such as OnTextChanged and then look at the first character in the string using IsDigit(Text,0) - I'll check when I get home in a couple of hours
0
 

Author Comment

by:bruwmac
ID: 6418854
TextChanged event was a good clue.
I did the following:

txtString1_TextChanged (...etcetera)
{
   try
   {
      int.Parse(txtString1.Text);
      MessageBox.Show("Please, do not enter numbers!");
   }
   catch(System.FormatException) // when a non-numeric is entered
   {
   }
}

This works slick. No IsAnything required. But, I ask you, is this proper programming, or am I quietly creating unknown problems? I don't want to blow-up any NASA rockets.

   
 
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 100 total points
ID: 6419347
its a known way - some people wait till you click on OK and then validate the fields entered others might handle key presses and not let you enter a number till the end

I think a good way would be to validate when the edit control loses focus eg (ps I have just found the events (the lightening symbol) I knew it had to be in there somewhere :) )

          protected void textBox1_Leave (object sender, System.EventArgs e)
          {
               // do you checin in here
               // if bad text you can throw focus back to the control to make them reenter it
               
          }
0
 

Author Comment

by:bruwmac
ID: 6419663
Thanks for the tip. I guess that wraps this topic up.
I have a couple of dialog boxes I add to most of my projects and I find myself recreating them over and over again. I know I can import them somehow. Has not worked, though. Missing references or something. An answer to my similar question, Combining projects in C#, in the general programming section would help me out with this.
Take care.

bruwmac
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now