2010/6/28 nisalm <[email protected]>:
> I got an error called  {"Index was outside the bounds of the array."}
>
> my code is looks like this.....
>
>
> using System;
> using System.Collections.Generic;
> using System.Linq;
> using System.Text;
>
>
> namespace Switch
> {
>    class SwitchCaseExample
>    {
>           static void Main(string[] userInput)
>        {
>            int input = int.Parse(userInput[0]);
>
>                 switch (input) // what is input
>            {
>                case 1: //if it is 1
>                    Console.WriteLine("You typed 1 (one) as the first
> command line argument");
>                    break; // get out of switch block
>                case 2: // if it is 2
>                    Console.WriteLine("You typed 2 (two) as the first
> command line argument");
>                    break; // get out of switch block
>                case 3: // if it is 3
>                    Console.WriteLine("You typed 3(three) as the first
> command line argument");
>                    break; // get out of switch block
>                default: // if it is not any of the above
>                    Console.WriteLine("You typed a number other than
> 1,2 and 3");
>                    break; // get out of switch block
>
>            }
>        }
>    }
> }
>
>

Hello,

The problem is that you're refering to userInput[0] and this will only
be executed successfully when the user cals your application like
this:

"Switch.exe ________"

If that happens, then you have "________" in the position 0 of
'userInput' array.
If not, then the array will not contain any items, and you should
check for that before refering to their elements. Something like:

if (userInput.Length == 0)
{
   Console.WriteLine("Something here...")
   return;
}

or just iterate over a infinite loop while user doesn't specify a parameter.

Hope this help.

-- 
Oscar Mederos

Reply via email to