0

I have an app that is used to run multiple reports that are then chosen by the command line argument upon run time (in scheduled Task). However, I want to be able to run it manually in visual studio and then ask me what report I want to run. Here is the code I tried:

static void Main(string[] args)
{
      var TestVersion = false;
      int HeaderRow = 2;

      if (args.Length==0)
      {
           Console.WriteLine("What Report would you like to run?");
           args[1] = Console.ReadLine();
      }

It crashes on the args[1] line with the following error:

Index was outside the bounds of the array.

I tried with args[0] as well and that didn't work either. I also tried withe args passed as a normal string. How do I fix this?

Bruno Ferreira
  • 466
  • 7
  • 17
djblois
  • 963
  • 1
  • 17
  • 52
  • Do you understand how arrays work? – Kenneth K. Mar 02 '17 at 18:04
  • @KennethK., not an expert but pretty well. – djblois Mar 02 '17 at 18:09
  • Arrays can't grow without being re-allocated. Instead of performing your operations on the array, create a kind of "args object" (a simple custom class in your application). It can be initialized from the `args` array on its constructor, for example. Then check the object to see if values were specified and set those values from your prompts accordingly. Trying to manually maintain the ordering and size of an array is just an unnecessary source of potential problems. (Heck, even a `Dictionary` could do the trick for holding the program's options.) – David Mar 02 '17 at 18:10

0 Answers0