Click here to Skip to main content
12,629,684 members (31,966 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hello experts,

First of all, thx for all the help so far. Now i have a small issue but i need some suggestions.
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 3-Jan-13 12:13pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Instead of Convert.ToInt32, you should use Int32.TryParse instead.
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.
}
  Permalink  
Comments
Gilmore Sacco 3-Jan-13 17:26pm
   
Thx sir, worked liek a charm :D
Marcus Kramer 3-Jan-13 17:31pm
   
You're welcome.
   
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
Marcus Kramer 4-Jan-13 10:22am
   
Thanks, Sergey.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I have some helper extension functions like:
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:
int option = Console.ReadLine().ToInt(1234);

Cheers
Andi
  Permalink  
v2
Comments
Marcus Kramer 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
Marcus Kramer 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
Marcus Kramer 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161205.3 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100