check console args

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,

my consoleApp.exe can take either 1, 2 or 3 parms what's the best way to
check for these conditions?

my guess is something like:

if (args.Length == 2)
{
//there were 2 parms passed
}
if (args.Length == 3)
{
//there were 3 parms passed
}
is that the best way?

thanks,
rodchar
 
rodchar said:
my consoleApp.exe can take either 1, 2 or 3 parms what's the best way to
check for these conditions?

my guess is something like:

if (args.Length == 2)
{
//there were 2 parms passed
}
if (args.Length == 3)
{
//there were 3 parms passed
}
is that the best way?

Depends on how you're going to use them. For example, iterating them may be
all you need - if you only care *what* the parameters are, and not really
how *many*. Or a switch statement based on the count. Or whatever makes
sense. Nothing magical here - you're just dealing with an array in any way
you wish.

foreach (string arg in args)
{
....
}
 
Not really a wrong or right, but I'd do this:

string param1 = string.Empty;

string param2 = string.Empty;

string param3 = "DEFAULTVALUE";



if (args.Length >= 1)

{

param1 = args[0];

}

if (args.Length >= 2)

{

param2 = args[1];

}

if (args.Length >= 3)

{

param3 = args[2];

}
 
Back
Top