Line: | Conditional Line | Type: | Walkthrough |
You should have completed: | Math Functions | This topic leads to: | Happiness Meter |
So far, if the user has entered a string or a number with a decimal point for an input where we asked for an integer, our programs have crashed. Proper exception handling will be introduced later in the module, but there is a set of functions provided to us that can make user input more robust.
Note that .Net has its own names for the variable types we use, so int is Int32, float is Single and so on. There is more information about this mapping on the C# reference site.
Note that this method returns a Boolean value telling us if it was successful or not. We also need the out keyword to allow the method to alter the value in the variable we pass in. Normally a value type variable passed to a method is copied and thus the original value remains unchanged.