Solved

Conversion problem

Posted on 2011-02-25
11
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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