Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Check for not numeric

Posted on 2007-10-10
15
Medium Priority
?
276 Views
Last Modified: 2008-01-09
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) ??
0
Comment
Question by:MikeMCSD
  • 4
  • 4
  • 3
  • +3
15 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 800 total points
ID: 20050334
double result = 0;
if (txtTwo.Text.Trim().Length == 0 || !double.TryParse(txtTwo.Text, out result)
{
}

Bob
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 20050337
you could add a ref to Microsoft.VisualBasic

then
Microsoft.VisualBasic.Information.IsNumeric(myVal)
0
 
LVL 25

Assisted Solution

by:SStory
SStory earned 400 total points
ID: 20050350
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
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
LVL 16

Author Comment

by:MikeMCSD
ID: 20050419
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
 
LVL 35

Expert Comment

by:YZlat
ID: 20050440
if (txtTwo.Text.Trim() == "") {

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

    }

}
0
 
LVL 19

Expert Comment

by:Limbeck
ID: 20050592
       double dummy=0;
        if ((txtTwo.Text.Trim() == "") || (!Double.TryParse(txtTwo.Text,out dummy)))
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 20051145
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20051198
What about decimal points, negative signs, and commas?  You can handle these with NumberStyles enumeration and TryParse.

Bob
0
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 800 total points
ID: 20051200

I would suggest this instead:

public bool IsNumber(string sNumber)
{
    return Regex.IsMatch(sNumber,"^\\d+$"));
}
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20051212
That also doesn't allow for decimals and negative signs.

Bob
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 20051246
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
 
LVL 35

Expert Comment

by:YZlat
ID: 20051291
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
 
LVL 35

Expert Comment

by:YZlat
ID: 20051307
for positive or negative integers use:

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

Expert Comment

by:SStory
ID: 20051952
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
 
LVL 16

Author Comment

by:MikeMCSD
ID: 20052480
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

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

581 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