Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# methods

Status
Not open for further replies.

mrkan

Programmer
Oct 30, 2006
39
0
0
US
What's wrong with this code?
I am getting that name IsNumeric does not exist in current contex. I found many examples like these online and non of them are working. So, how can I called this method properly.
As far as I know, it should be static in order to call it directly: bool isNumeric = IsNumeric("123abc456").

If method is not static, what do I do? Thanks.


Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Test
{
    class Program
    {
        public bool IsNumeric_Regex(object num)
        {
            Regex r = new Regex(@"(^[-+]?\d+(,?\d*)*\.?\d*([Ee][-+]\d*)?$)|(^[-+]?\d?(,?\d*)*\.\d+([Ee][-+]\d*)?$)");
            return r.Match(num.ToString()).Success;
        }
        static void Main(string[] args)
        {
            [b]bool isNumeric = IsNumeric("123abc456");[/b]
            Console.Write("Press Enter to Exit!");
            Console.Read();
        }
    }
}

[code]
 
You do not have a method called IsNumeric...

you do have a method called IsNumeric_Regex..


But here's the bigger issue - your IsNumeric_Regex method is not static. This means you need to create an instance of the class before you can call it.


An example:

public class Car
{
//Don't ask me why you would do this exactly... :)
public static bool IsCar()
{
return true;
}

public bool IsVehicle()
{
return true;
}
}


When you try to use this class now, you will notice 2 things.

1. IsCar is static, so your code will look like:
Car.IsCar();

2. You can't access IsVehicle() unless you have an instance of Car.

Car c = new Car();
c.IsVehicle();
c.IsCar(); <-- this is not available


I think that's the quickest way I can explain it - although I did just watch SuperBad - that movie will screw with your head at 1:30am.

 
you could use double.TryParse which would return a bool of whether or not you can convert the object inton a double
 
IsNumeric sounds like it's from VB.NET...

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Here is what you should do:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Test
{
    class Program
    {
        public bool IsNumeric_Regex(object num)
        {
            Regex r = new Regex(@"(^[-+]?\d+(,?\d*)*\.?\d*([Ee][-+]\d*)?$)|(^[-+]?\d?(,?\d*)*\.\d+([Ee][-+]\d*)?$)");
            return r.Match(num.ToString()).Success;
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            bool isNumeric = p.IsNumeric_Regex("123abc456");
            Console.Write("Press Enter to Exit!");
            Console.Read();
        }
    }
}
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top