Solved

IsNumber method in C#?

Posted on 2001-08-22
9
3,744 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:bruwmac
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now