Check for not numeric

Again, I can do this in VB but C# loves to drive me nuts.

I want to check for an empty string OR if not numeric

      if (txtTwo.Text.Trim() == "")    OR (txtTwo.Text is not numeric) ??
LVL 16
MikeMCSDAsked:
Who is Participating?
 
Bob LearnedCommented:
double result = 0;
if (txtTwo.Text.Trim().Length == 0 || !double.TryParse(txtTwo.Text, out result)
{
}

Bob
0
 
burakiewiczCommented:
you could add a ref to Microsoft.VisualBasic

then
Microsoft.VisualBasic.Information.IsNumeric(myVal)
0
 
SStoryCommented:
Try this
http://www.dotnet247.com/247reference/msgs/27/135843.aspx

HTH

Just my 2 cents worth:
I can't see any reason for wanting to program in C# for most things that I do.  VB is so much more productive.  I will admit that graphic image processing and such would be easier in C#, but for other things if you already know VB, why switch?

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
MikeMCSDAuthor Commented:
thanks everyone . .

>> but for other things if you already know VB, why switch?
A job!   Take a look at the help wanted ads.  Most companies are C#.
80% of them are C#, or VB AND C#.
At my last job I was using VB but the company was changing to C#.
I like using VB and I'm really not liking C#.
I am just going to have to bite the bullet and learn it.
0
 
YZlatCommented:
if (txtTwo.Text.Trim() == "") {

}
else
{
   if (! (Regex.IsMatch(txtTwo.Text,"^\\d+$"))
   {

    }

}
0
 
LimbeckCommented:
       double dummy=0;
        if ((txtTwo.Text.Trim() == "") || (!Double.TryParse(txtTwo.Text,out dummy)))
0
 
MikeMCSDAuthor Commented:
this worked:

double result = 0;
if (txtOne.Text.Trim() == "" || !double.TryParse(txtOne.Text, out result))  


I came across this that also looked interesting:

public bool IsNumber(string sNumber)
    {
    bool Result = true;
    for (int i = 0; i < sNumber.Length; i++)
      {
      if (!Char.IsNumber(sNumber, i))
        {
        Result = false;
        }
      }
    return Result;
    }
0
 
Bob LearnedCommented:
What about decimal points, negative signs, and commas?  You can handle these with NumberStyles enumeration and TryParse.

Bob
0
 
YZlatCommented:

I would suggest this instead:

public bool IsNumber(string sNumber)
{
    return Regex.IsMatch(sNumber,"^\\d+$"));
}
0
 
Bob LearnedCommented:
That also doesn't allow for decimals and negative signs.

Bob
0
 
MikeMCSDAuthor Commented:
I just need to check for an Integer, and it must be an Integer.
Would:

public bool IsNumber(string sNumber)
    {
    bool Result = true;
    .......

be better for this situation?

0
 
YZlatCommented:
this should account for both positive and negative, as well as decimals:
public bool IsNumber(string sNumber)
{
    return Regex.IsMatch(sNumber,"^-{0,1}\d*\.{0,1}\d+$"));
}
0
 
YZlatCommented:
for positive or negative integers use:

public bool IsNumber(string sNumber)
{
    return Regex.IsMatch(sNumber,"^[-+]?\d*$");
}
0
 
SStoryCommented:
Mike,

I understand. A job is a pretty good reason to switch. It is ashame that so many companies have decided that C# is superior.  I honestly find it no where near as productive.  Oh well I better stop before I get into a flame war with someone.

letting an exception be thrown is costly performance wise and isn't something that is really desirable.

If I had to work with C# I'd write my on IsNumeric I guess (back to the good old days of C where you get to write everything. ;)

You'd just loop through the string and make sure all digits are 0-9, no more and one "." is present and a possible "-" at the beginning.
0
 
MikeMCSDAuthor Commented:
SStory, I agree.
I guess C# is being used more because it's compared to Java and C++ and
Object Oriented principals.
I guess once I start using more it won't be so bad; it's really
just the same as VB except for the syntax.
Applying and understanding OOP when using C# is the real challenge.
I understand the basics of OOP, but have trouble with the advanced stuff.
practice practice practice is the only way
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.