none
I'm having error when I start compiling my C # project RRS feed

  • Question

  • look at my error, please try to fix it

    look this print

    https://prnt.sc/qpiw2z

    look at my mistake, please try to fix it
    Saturday, January 18, 2020 4:46 PM

All replies

  • "index out of range" when accessing args[0] means that there was nothing in args. You probably forgot to pass an argument when you started the program. To pass a parameter in debug mode, you can add it in the Project Properties, under Debug -> Program Arguments.

    Saturday, January 18, 2020 5:46 PM
    Moderator

  • but put what's in there?
    • Edited by yFlameBR Saturday, January 18, 2020 5:59 PM
    • Marked as answer by yFlameBR Saturday, January 18, 2020 7:00 PM
    • Unmarked as answer by yFlameBR Saturday, January 18, 2020 7:00 PM
    Saturday, January 18, 2020 5:58 PM
  • Since you posted an image the best I can do is indicate to you to assert there is at least one argument, same idea shown in Microsoft documentation.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                if (args.Length >0)
                {
                    /*
                     * We have at least one argument, display it
                     */
                    Console.WriteLine($"First arg: {args[0]}");
                }
                else
                {
                    Console.WriteLine("No arguments passed in");
                }
                Console.ReadLine();
            }
    
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, January 18, 2020 9:18 PM
    Moderator
  • Hi yFlameBR,

    Thank you for posting here.

    There is basically no other possibility for this error. This program needs to pass some parameters when calling. Do you run this program directly from Visual Studio?

    We don't know the function of your program, nor do we know what parameters need to be passed in, but I wrote a simple example, you can refer to it.

           static void Main(string[] args)
            {
                Console.WriteLine($"Hello {args[0]}!") ;
    
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }

    If you want to run this program directly in Visual Studio, follow these steps.

    Right click on the project (not the solution)-> Properties-> Debug-> Command line arguments.

    What Aalberto said is different from what I said, probably because Visual Studio version is different.

    If you need to call this program in other programs (I think this may be the correct way to use this program), you can refer to the following code:

            static void Main(string[] args)
            {
                try
                {
                    using (Process myProcess = new Process())
                    {
                        myProcess.StartInfo.UseShellExecute = false;
                        myProcess.StartInfo.FileName = "d:\\ConsoleApp1.exe";
                        myProcess.StartInfo.Arguments = "Timon";
                        myProcess.Start();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            } 

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 20, 2020 2:27 AM