Re: long input

From: Branimir Giurov (branimir_at_nospam.com)
Date: 08/27/04


Date: Fri, 27 Aug 2004 10:44:13 +0300

Hi there,

you should save user's input as a string, then either try to parse it with
long.Parse(string) or first validate the string (with RegEx for example) and
then parse it if the string's valid input for a long value. If by any chance
the conversion doesn't work, you'll recive a FormatException, so it might be
a good idea to catch it. Something like the following:

long getInput()
{
    while ( true )
    {

    string input = Console.ReadLine();

    try
    {
        long value = long.Parse(input);
        return value;
    }
    catch ( FormatException )
    {
        Console.WriteLine("Invalid input. Please enter a valid long value");
    }

    }
}

Cheers,
Branimir

-- 
Branimir Giurov
MCSD.NET, MCDBA
www.sofiadev.org
"Dave" <better_cs_now@yahoo.com> wrote in message
news:10iteqt5gia542e@news.supernews.com...
>
> What's the proper way to get a long from the console?
>
>


Relevant Pages

  • Re: How to check a given string is float-point in (c#) ?thanx
    ... > Double.praseto check the given string contain invalid char or not,if it ... > throws FormatException, the given string is invalid, if it throws ... To parse numbers with exponents you must supply a NumberStyles parameter ... addition you should pass a CultureInfo instance since all of your ...
    (microsoft.public.dotnet.languages.csharp)
  • regex help
    ... I'm trying to write a regex to parse the following data. ... Each group is a string ... The format is '$123.45' with the possibility of trailing HTML before ...
    (perl.beginners)
  • Re: regex help
    ... Roberto Etcheverry wrote: ... >>I'm trying to write a regex to parse the following data. ... Each group is a string ...
    (perl.beginners)
  • Re: Newbie: Parsing help
    ... >I know very little about regex, but I think it can help me now. ... >I want to parse this string and get two new strings ... Mike Slass ...
    (comp.lang.perl.misc)
  • Re: Help With PyParsing of output from win32pdhutil.ShowAllProcesses()
    ... None to a string). ... Still have an urge to parse with pyparsing? ... pyparsing Dict class. ... keys from the parsed data, so it looks like you are already thinking ...
    (comp.lang.python)