Solved

Conversion problem

Posted on 2011-02-25
11
293 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

17 Experts available now in Live!

Get 1:1 Help Now