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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Index was outside the bounds of the array 1

Status
Not open for further replies.

Jaheel22

Technical User
Jul 14, 2004
84
US
I get the following exception / error

"Index was outside the bounds of the array"

whenever i excute the following console application from command line. I have Windows XP professional on my computer. Please assist. Thanks

using System;
using System.Collections.Generic;
using System.Text;

namespace _2303
{
class Program
{
static void Main(string[] args)
{
switch (args[0])
{
case "scott":
Console.WriteLine("Scott was passed in.");
break;
case "jim":
Console.WriteLine("Jim was passed in.");
break;
default:
Console.WriteLine("Done.");
break;
}
}
}
}
 
Are you passing any command line argument to your application? 'Cause if you're not, then args would have the Length equal to zero, and thus args[0] would not exist...

You need to add some code before the switch:
Code:
if( args.Length == 0)
{
  Console.WriteLine( "At least one argument is required.");
  return;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top