Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello experts,

First of all, thx for all the help so far. Now i have a small issue but i need some suggestions.
C#
int option;  option = Convert.ToInt32(Console.ReadLine());


If i enter a number from 0-9 all works great, but if i enter a letter an error promts. Any suggestions pls?
Posted

Instead of Convert.ToInt32, you should use Int32.TryParse instead.
C#
int option;
string inValue = Console.ReadLine();
if( Int32.TryParse(inValue, out option) )
{
   // It converted successfully
}
else
{
   // Apply some default value or prompt the user that an invalid value was entered.
}
 
Share this answer
 
Comments
Gilmore Sacco 3-Jan-13 17:26pm    
Thx sir, worked liek a charm :D
fjdiewornncalwe 3-Jan-13 17:31pm    
You're welcome.
Sergey Alexandrovich Kryukov 3-Jan-13 19:06pm    
I always recommend to prefer Parse and TryParse to "converting" from string, because the word "Parse" is self-explanatory, in a correct way, showing what's really happens. And I don't really remember if I ever used Convert. What am I talking about. Ah, remembered: my 5. :-)
—SA
fjdiewornncalwe 4-Jan-13 10:22am    
Thanks, Sergey.
The code is working fine. Convert.ToInt32 is useless unless you KNOW the input is an int. Otherwise, get the value and use int.TryParse.
 
Share this answer
 
I have some helper extension functions like:
C#
public static class Extensions
{
    /// <summary>Tries to parse a numeric value from this string.</summary>
    /// <remarks>In case of error, takes the given defaultValue.</remarks>
    /// <param name="defaultValue">Fallback in case of not matching input.</param>
    /// <returns>the parsed value or, in case of parsing errors, the defaultValue</returns>
    public static int ToInt(this string s, int defaultValue)
    {
        int value = int.TryParse(s, out value) ? value : defaultValue;
        return value;
    }
    /// <summary>Tries to parse a numeric value from this string.</summary>
    /// <remarks>In case of error, takes the given defaultValue.</remarks>
    /// <param name="defaultValue">Fallback in case of not matching input.</param>
    /// <returns>the parsed value or, in case of parsing errors, the defaultValue</returns>
    public static double ToDouble(this string s, double defaultValue)
    {
        double value = double.TryParse(s, out value) ? value : defaultValue;
        return value;
    }
    ...
}


To be used like this:
C#
int option = Console.ReadLine().ToInt(1234);


Cheers
Andi
 
Share this answer
 
v2
Comments
fjdiewornncalwe 4-Jan-13 10:24am    
I've done this as well at a previous employer. It definitely simplifies the process. The only thing I believe I did differently was to include an explicit defaultValue so that the argument becomes optional.
Andreas Gieriet 4-Jan-13 10:47am    
Hello Marcus,
In C#, I have moved away from default values in public/protected methods.
Reason: the default parameters are compiled into the client code (not into the defining library as in C++) :-(
This "stupid" behaviour renders the default value handling somehow useless for libraries.
The same holds for named parameters and public/protected const members.
For optional parameters I prefer function overload and chaining the call explicitly.
For the const members, I prefer now static readonly where applicable and where it makes sense.
Cheers
Andi
fjdiewornncalwe 4-Jan-13 12:20pm    
Interesting. I didn't know that. I'll have to look into that myself as well.
Andreas Gieriet 4-Jan-13 13:20pm    
See C# In Depth – Optional Parameters and Named Arguments and go to the section "Versioning and optional parameters".
Cheers
Andi
fjdiewornncalwe 4-Jan-13 13:42pm    
Will do. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900