Solved

Conversion problem

Posted on 2011-02-25
11
290 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Using Simpleject with Class Library 11 25
C# Export DataGridView 4 39
asp.net bundle 8 36
ASP.NET Web API or ASP.NET Core MVC? 3 25
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

22 Experts available now in Live!

Get 1:1 Help Now