I have a C# .NET framework 4.7.2 C# console application that is reading up to 20 OPC DA items from a remote OPC DA server. I call this console app from another .NET app with arguments.The first argument defines the amount of items that should be read, the rest 20, the items. I want that in the parent app where I call this console app, I just write (additional to the first argument) the needed amount of arguments (for three items just 3). But in my curent code, I have to define the rest 17 arguments at least as x (although not used) , otherwise I get following Error on the console app:

Description: The process was terminated due to an unhandled exception.
Exception Info: System.IndexOutOfRangeException
at CNCOnline_OPC_DA.Program..ctor()
at CNCOnline_OPC_DA.Program.Main(System.String[])

How can I prevent this? I mean the first 2 arguments should be like mandatory (amount of items, and at least one item), and the rest 19 optionally.

Parent app:

Process Process_OPCDA = new Process
{
   StartInfo = new ProcessStartInfo
   {
   FileName = TagService_MTX.MTX_OPC_DA_reader_path,
   Arguments = "3 item1 item2 item3 x x x x x x x x x x x x x x x x x"
   UseShellExecute = false,
   RedirectStandardOutput = true,
   CreateNoWindow = true
   }
};
Process_OPCDA.Start();

Console app with arguments:

    public string item_no = Environment.GetCommandLineArgs()[1];
    public string item_1 = Environment.GetCommandLineArgs()[2];
    public string item_2 = Environment.GetCommandLineArgs()[3];
    public string item_3 = Environment.GetCommandLineArgs()[4];
    //...all 20 defined here
    public string item_20 = Environment.GetCommandLineArgs()[21];

    static void Main(string[] args)
    {
        for(int b=0;b<20;b++)
        {
            if(Environment.GetCommandLineArgs()[b+2].Length > 0) 
            { 
                item_array[b]= Environment.GetCommandLineArgs()[b+11];
            }
            else
            {
                item_array[b] = "";
            }
        }

        // rest code...
    }
1

There are 1 best solutions below

0
PMF On BEST ANSWER

Don't use Environment.GetCommandLineArgs() if you have access to the args parameter of the Main method, that is just confusing. Also don't make assumptions about the number of arguments given (or at least check that before). I suggest you change your method like this:

    // .... 
    public string item_20;

    static void Main(string[] args)
    {
        if (args.Count < 20)
        {
            Console.WriteLine("Not enough parameters");
            return;
        }
        item_1 = args[0];
        item_2 = args[1]; // etc... Instead, you could also create a copy of the array and keep the indexing
        for(int b=0;b<18;b++)
        {
            // You might eventually need to consider it an error if 
            // the b + 11 doesn't exist.
            if(args[b+2].Length > 0 && b + 11 < args.Count) 
            { 
                item_array[b]= args[b+11];
            }
            else
            {
                item_array[b] = "";
            }
        }

        // rest code...
    }

Your for loop also looks like there's some ambiguity in how your parameters are defined. How do you know how many parameters you expect? It kind of looks as if your input string is really complex, so try to use either a command line parsing library with named arguments or probably better yet, use a file to provide the input to your program.