none
Index was outside the bounds of the array C# RRS feed

  • Question

  • I am new to C#, and I don't understand why I am getting this... as I feed 8 arguments and only go up to args[7]... I don't see where the index goes out of bounds.

    The error message says it is either happening at line "Main(string[] args)" or at the that array... not sure how to read it.

    Any help would be appreciated. It runs fine in the Visual Studio but throws the error when I run the compile program.

    I removed some code about connecting to Exchange Web Services which I believe to be irrelevant.

    //Arguments: CSN000040 "Company Name" "TEST 19" "7/27/2013 11:30:00 AM" "Company Name" ghanssen@domain.com "ghanssen" 90
            static void Main(string[] args)
            {
                string apptSubject = args[0];
                string apptBody = args[1];
                DateTime apptDateTime = DateTime.Parse(args[3]);
                string apptMailbox = args[5];
                Double apptDuration = Convert.ToDouble(args[7]);
               
                service.ImpersonatedUserId = new  ImpersonatedUserId(ConnectingIdType.SmtpAddress, apptMailbox);
    
                Appointment appointment = new Appointment(service);
                appointment.Subject = apptSubject;
                appointment.Body = apptBody;
                appointment.Start = apptDateTime;
                appointment.End = appointment.Start.AddHours(apptDuration/60);
                appointment.Save(SendInvitationsMode.SendToNone);
            }

    This is a console application.

    I am running it with the following command line:

    setup.exe CSN000040 "Company Name" "TEST 19" "7/27/2013 11:30:00 AM" "Company Name" ghanssen@domain.com "ghanssen" 90

    • Edited by ghanssen Sunday, July 28, 2013 5:41 PM Edit
    Sunday, July 28, 2013 12:13 PM

Answers

  • Unless your project name is setup, I don't think setup.exe is the right executable.
    • Marked as answer by ghanssen Sunday, July 28, 2013 6:26 PM
    Sunday, July 28, 2013 5:57 PM

All replies

  • Put the following code at the beginning of your Main method to assist you in identifying your issue:

       if (args.Length < 8)
       {
        Console.WriteLine("Invalid number of arguments");
        for (int l_count = 0; l_count < args.Length; l_count++)
        {
         Console.WriteLine("Argument " + l_count + ": " + args[l_count]);
        }
       }
    BTW, args[7] is the eighth element.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.


    • Edited by TSoftware-Old Sunday, July 28, 2013 1:19 PM Corrected number of arguments
    Sunday, July 28, 2013 1:15 PM
  • Could you show the actual line of code you use to call this with the arguments? Are you running it from a command line or from another application?

    ~~Bonnie Berent DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, July 28, 2013 4:37 PM
    Moderator
  • setup.exe CSN000040 "Company Name" "TEST 19" "7/27/2013 11:30:00 AM" "Company Name" ghanssen@domain.com "ghanssen" 90

    this worked before... then I started assigning more arguments to variables one by one... I didn't notice any direct link as I assigned them though... I took them off one by one as well while troubleshooting... without result... I have not tried using less arguments or different ones, only assigning them.

    I am using setup.exe because I didn't see any other .exe that came out of the publishing / install process.

    Sunday, July 28, 2013 5:36 PM
  • I added this... and when I run it, it gives me: Invalid number or Arguments

    Oddly... I added several more arguments and got the same result... so its almost as if one of the arguments is exiting the input process before it reaches the 8th argument?

    Sunday, July 28, 2013 5:51 PM
  • Unless your project name is setup, I don't think setup.exe is the right executable.
    • Marked as answer by ghanssen Sunday, July 28, 2013 6:26 PM
    Sunday, July 28, 2013 5:57 PM
  • If it still runs fine within the VS environment, perhaps you are encountering a conflict with the name "setup.exe" - recompile your program, supplying a unique name for the .exe temporarily (like setuptemp.exe or something) and try that.  Post back the results - including an exact error message if you get one...

    BitWise_

    Sunday, July 28, 2013 5:58 PM
  • So I dug through the project settings and found where the actual executable was going... and hey! it works just fine!

    So no bug here, just a human.

    Thanks everyone!

    Sunday, July 28, 2013 6:26 PM