Home All Groups Group Topic Archive Search About
Author
30 Jun 2009 8:18 PM
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

Author
30 Jun 2009 8:29 PM
proxyuser
Show quote Hide quote
"rodchar" <rodc***@discussions.microsoft.com> wrote in message
news:41289996-CAD0-4DD6-87F1-BA9BBC96D0AB@microsoft.com...
>
> 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)
{
....
}
Are all your drivers up to date? click for free checkup

Author
30 Jun 2009 8:33 PM
sloan
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];

}









Show quoteHide quote
"rodchar" <rodc***@discussions.microsoft.com> wrote in message
news:41289996-CAD0-4DD6-87F1-BA9BBC96D0AB@microsoft.com...
> 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
Author
30 Jun 2009 8:59 PM
rodchar
thanks all for the help,
rod.

Show quoteHide quote
"rodchar" wrote:

> 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

Bookmark and Share