Solved

Conversion problem

Posted on 2011-02-25
11
294 Views
Last Modified: 2012-05-11
Hi,
I converted this code from VB to C# but it's still not working ... getting the following error: The name 'IsNumeric' does not exist in the current context
How I can make it to work in C#. Please advise. Thanks!

if ((IsNumeric(txtTax.Text) == false)) {
        txtTax.Text = 0;
    }
    else if ((txtTax.Text == "")) {
        txtTax.Text = 0;
    }
0
Comment
Question by:avi7
  • 5
  • 5
11 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34985078
I believe this was asked earlier today (?) but here's the low-down:

There is no IsNumreric in C#.

Here is a very good discussion of what you can do instead.

It involves using try/catch blocks:

http://www.codeproject.com/KB/cs/csharp-isnumeric.aspx
0
 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 100 total points
ID: 34985090
There is also a way that is described in the reference above that allows you to call the VisualBasic IsNumeric function from C# with a "using" statement.  Try this first since it may be the easiest solution.

using Microsoft.VisualBasic;

...


if ((Information.IsNumeric(txtTax.Text) == false)) {
        txtTax.Text = 0;
    }
    else if ((txtTax.Text == "")) {
        txtTax.Text = 0;
    }

Open in new window

0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34985107
Use it this way:
for (int i = 0; i < textBox1.Text.Length; i++)
                if (!char.IsNumber(textBox1.Text, i))
                    MessageBox.Show("Not Number");

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 16

Expert Comment

by:sjklein42
ID: 34985124
sergiobg57, Does that handle numbers with minus signs and decimal points?
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34985156
No, it doesn't, but it's adaptable for that purpose as well.
for (int i = 0; i < textBox1.Text.Length; i++)
                if (!char.IsNumber(textBox1.Text, i) && textBox1.Text[i] != '.' && textBox1.Text[i] != '-')
                {
                    MessageBox.Show("Not a Number");
                    break;
                }

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34985224
It may not matter, but sergiobg57's test isn't the same as IsNumeric.

IsNumeric checks for a valid number, with a single decimal point (if any), and check that the minus sign is the first character, if present.  I also think it enforces at least one digit before the decimal point but am not sure about that.
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34985234
Yeah, i see.
But as i said, everything is adaptable.
C# is a great language and you'll have freedom to do whatever is needed in fact.

Having the logic, you can recreate the same effects IsNumeric has.
But for his code, i believe it would be too much.

Any way, just for fun and profit...
bool Decimal = false;
            for (int i = 0; i < textBox1.Text.Length; i++)
                if (!char.IsNumber(textBox1.Text, i))
                {
                    if((0 != i && '-' == textBox1.Text[i]) ||
                        ('.' == textBox1.Text[i] && Decimal))
                    {
                        MessageBox.Show("Not Number");
                        break;
                    }
                    
                    
                    if ('.' == textBox1.Text[i] && !Decimal)
                        Decimal = true;
                }

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34985262
sergiobg57,  Looks good.  A useful piece of code, and more straightforward to use than cross-calling to the VB runtime.
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34985278
In fact, it was wrong.
During the night i'm not a good programmer.

Any way, a more readable code and free of bugs.


bool Decimal = false, NaN = false;
            for (int i = 0; i < textBox1.Text.Length; i++)
                if (!char.IsNumber(textBox1.Text, i))
                {
                    //only accepts minus as the first char
                    if ('-' == textBox1.Text[i] && i != 0)
                        NaN = true;
                    
                    //decimal points cannot be duplicated
                    if('.' == textBox1.Text[i] && Decimal)
                        NaN = true;
                    
                    //assure decimal point isn't duplicated
                    if ('.' == textBox1.Text[i] && !Decimal)
                        Decimal = true;


                    //if its not a number
                    if (NaN)
                    {
                        MessageBox.Show("Not a Number");
                        break;
                    }
                }

Open in new window

0
 

Author Comment

by:avi7
ID: 34985541
Thanks!

0
 
LVL 3

Accepted Solution

by:
sergiobg57 earned 400 total points
ID: 34985550
Last comment.
I've added it to a function(IsNumeric) and now it makes his code work.

 
public bool IsNumeric(String text)
        {
            bool Decimal = false, NaN = true;
            for (int i = 0; i < text.Length; NaN = true, i++)
                if (!char.IsNumber(text, i))
                {
                    //only accepts minus as the first char
                    if ('-' == text[i] && 0 == i)
                        NaN = false;

                    //decimal points cannot be duplicated
                    if ('.' == text[i] && !Decimal)
                        NaN = false;

                    //assure decimal point isn't duplicated
                    if ('.' == text[i] && !Decimal)
                        Decimal = true;


                    //if its not a number
                    if (NaN)
                    {
                        return false;
                    }
                }
            return true;
        }

Open in new window


In his code:
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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