Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Conversion problem

Posted on 2011-02-25
11
Medium Priority
?
301 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1600 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

721 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