Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IsNumeric method in MonoDevelop by using Microsoft.VisualBasic reference?

What I am trying to do is to check whether the float input is a number or not. I am being asked to do so by using IsNumeric() method. The problem is that I am using MonoDevelop and I can't figure out why this doesn't work. It seems like I have added assembly reference which I need.

So from scratch. How do I do this? Do I have to add something to VB assembly reference? And, If that will still work when I will try to work in school on VisualStudio?

static void getBookInfo(Book book)
{
        Console.Write("Enter Book Title: ");
        book.Title = Console.ReadLine();
        Console.Write("Enter Author's First Name: ");
        book.AuthorFirstName = Console.ReadLine();
        Console.Write("Enter Author's Last Name: ");
        book.AuthorLastName = Console.ReadLine();
        Console.Write("Enter Book Price: $");
        book.Price = float.Parse(Console.ReadLine());
}

The reference file with VB looks like this:

public class VBCodeProvider : CodeDomProvider
{
    // Constructors
    public VBCodeProvider ();
    public VBCodeProvider (IDictionary<string, string> providerOptions);

    // Methods
    public virtual ICodeCompiler CreateCompiler ();
    public virtual ICodeGenerator CreateGenerator ();
    public virtual TypeConverter GetConverter (Type type);
    public virtual void GenerateCodeFromMember (CodeTypeMember member, TextWriter writer, CodeGeneratorOptions options);

    // Properties
    public virtual string FileExtension { get; }
    public virtual LanguageOptions LanguageOptions { get; }
}

Regards. And thank you for help.

Some more info. On top of my page I do have:

using Microsoft.VisualBasic;

IsNumeric() method isn't listed when I try to type it (usually things are listed).

Another edit. So I could do this this way (but I do need to use the IsNumeric method so I wont loose any points from homework):

static void getBookInfo(Book book)
{
    bool isNumeric;
    float number;
    string numberInput;

    Console.Write("Enter Book Title: ");
        book.Title = Console.ReadLine();
        Console.Write("Enter Author's First Name: ");
        book.AuthorFirstName = Console.ReadLine();
        Console.Write("Enter Author's Last Name: ");
        book.AuthorLastName = Console.ReadLine();
        Console.Write("Enter Book Price: $");
        numberInput = Console.ReadLine();

    isNumeric = float.TryParse(numberInput, out number);

    if (isNumeric)
        Console.WriteLine(number.ToString());
    else
        Console.WriteLine("not number");
}
like image 968
HelpNeeder Avatar asked Nov 05 '22 13:11

HelpNeeder


2 Answers

It's a static method on the "Microsoft.VisualBasic.Information" class. Even with that using you need to access it as

Information.IsNumeric (someString)

See http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.information.isnumeric.aspx

like image 122
Mikayla Hutchinson Avatar answered Nov 14 '22 22:11

Mikayla Hutchinson


If the pain of attempting to link to the Visual Basic assemblies is too much, you might find the decimal.TryParse() method performs a suitably similar functionality with few drawbacks.

An example of its use can be found at http://msdn.microsoft.com/en-us/library/9zbda557.aspx

like image 45
lzcd Avatar answered Nov 14 '22 22:11

lzcd