Solved

IsNumber method in C#?

Posted on 2001-08-22
9
3,770 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

738 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