Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello World Problem

Status
Not open for further replies.

mkohl

Programmer
Feb 22, 2005
82
US
I know this is a simplex problem, but I can't figure this out. This is my first C# console program and it crashes at runtime.

I went thru all the code, that was in my book and did not find the error.

Error: Index out of range.
The error points to:
int iChoice = int.Parse(args[0]);

'code
class Greetings {
public static void DisplayEnglish() {
Console.WriteLine("Hello, world!");
}
public static void DisplayItalian() {
Console.WriteLine("Ciao, mondo!");
}
public static void DisplaySpanish() {
Console.WriteLine("Hola, imundo!");
}
}
delegate void delGreetings();
class HelloWorld {
static void Main(string [] args) {
int iChoice = int.Parse(args[0]);
delGreetings[] arrayofGreetings ={
new delGreetings(Greetings.DisplayEnglish),
new delGreetings(Greetings.DisplayItalian),
new delGreetings(Greetings.DisplaySpanish)};
arrayofGreetings[iChoice - 1]();
}
}



 
Are you running your project from the IDE? If so, did you set the command line parameter (a number) before running?

Check on Project/Properties for command-line parameters.
 
yes I am running it from the IDE. It was my assumition that I set the param at run time at the command prompt in the console.
 
no.

The arguments come from the initial call to your program.

In a dos console you would type

C:\>yourapplication.exe 100

and your first string in your arguments would be 100.

If you are looking to read a number from the console during runtime, then you want:

Console.Write("Enter A Number: ");
Console.ReadLine(); //and grab your integer value here
 
Pull up the properties for your project (right-click on the project in solution explorer) and go to the debugging tab. Enter your command-line value in the command-line entry.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top