Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

IsNumber method in C#?

Posted on 2001-08-22
9
Medium Priority
?
3,783 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 400 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

715 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