Friday, January 25, 2013

5.11. Parameters and return value for "Main"


It is usual than a "command line program" accepts options that the user indicates as arguments. For example, under Linux or other Unix-like operating system, we can see a detailed list of files ending in .c by

lsl *.c

In this case, the command is "ls", and the two options (arguments or parameters) are "-l" and "*. c".

The equivalent command in MSDOS and in Windows command line would be:

dir *.c

Now the command is "dir", and the parameter is "*. c".

Those options that are passed when calling the program can be read from C#. It is a special parameter for Main, an array of strings:

static void Main(string[] args)

To know the values for these parameters we would proceed in the same way as for any other array: using a "for" sentence, ending on the length of the array:

for (int i = 0; i < args.Length; i++)
{
    System.Console.WriteLine("The parameter {0} is: {1}",
      i, args[i]);
}


On the other hand, if we want our program to be interrupted at some point, we can use the "Environment.Exit" command. It can be used like this:

Environment.Exit(1);

In brackets we indicate a certain code, which is usually (by convention) a 0 if there has been no error, or a different code if an there has been any error.

This value can be checked from the operating system. For example, in MS-DOS and Windows it can be read with "IF ERRORLEVEL", like this:

IF ERRORLEVEL 1 ECHO There was a problem

An alternative way to indicate errors to the operating system is not declaring Main as "void" but as "int", and then using the command "return":

public static int Main(string[] args)
{
  ...
  return 1; 
}

An example all of this might be:

/*---------------------------*/
/*  C# Example #58:          */
/*  example58.cs             */
/*                           */
/*  Parameters and return    */
/*  value of "Main"          */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example58
{

  public static int Main(string[] args)
  {
    Console.WriteLine("parameters: {0}", args.Length);
   
    for (int i = 0; i < args.Length; i++)
    {
      Console.WriteLine("The parameter {0} is: {1}",
        i, args[i]);
    }
   
    if (args.Length == 0)
    {
      Console.WriteLine("No parameters!");
      Environment.Exit(1);
    }
   
    return 0;
 
  }
 
}


Suggested exercises:
·       (5.11.1) Create a program called "sum", to calculate (and show) the sum of two numbers that are indicated as parameters. For example, if the user types "sum 2 3", the program must answer "5", and if he types "sum 2", it must answer "not enough data" and return an error code 1.
·       (5.11.2) Create a basic calculator, to add, subtract, multiply or divide the two numbers indicated as parameters. Examples of use: "calc 2 + 3" or "calc 5 * 60".
·       (5.11.3) Create a variant of the exercise 5.11.2, where Main returns the code 1 if the indicated operation is invalid or code 0 when it is an acceptable operation.
·       (5.11.4) Create a variant of the exercise 5.11.3, which also Main returns the code 2 if one of the two numbers to operate does not have a valid numeric value.

No comments:

Post a Comment