Solved

Can't pass string [ ]  parmater to Main() in Windows Form Project in Visual Studio 2013

Posted on 2014-10-24
2
506 Views
Last Modified: 2014-10-24
I have a Windows Form project in VS2013. Here is the Main() function in the Program.cs file.

namespace CommandLineArgs
{
    static class Program
    {  
        [STAThread]
        static void Main(string[] args) // passing parameter args
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form frm = new Form1();
            if (args.Length == 0)
                frm.Text = "No arguments provided";
            else
            {
                for (int j = 0; j < args.Length; j++)
                    frm.Text += args[j] + " ";
            }
            Application.Run(frm);
        }
    }
}

The text at the top of the form window says "No arguments provided" because no values were provided for the args array. Suppose I want to add the strings "a", "b", "c" to args as parameters to Main(). I know I can do it  from the command line by running CommandLineArgs.exe a b c. I can also do it through the IDE by right clicking the project's name and going to  Properties > Debug > Command Line Arguments. But is there a way to add "a", "b", and "c" directly through code? I'd like to declare and initialize the args array before passing it into Main(). Below is what I tried, but it didn't work.

      string[ ] args = new string[ ] { "a", "b", "c" };
      static void Main(args)
      {
           //Main() code here
      }
0
Comment
Question by:MBarongan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40403554
I do not think it is possible, args is filled by the .NET Framework based on your external parameters.
If you need to define a default value (when args is not defined) then you should code directly inside Main as shown below:
 if (args.Length == 0)
                 frm.Text = "Your default value goes here";

Open in new window

0
 

Author Closing Comment

by:MBarongan
ID: 40403577
I didn't thing it was possible, but I though I'd ask anyway just in case someone knew how to trick VS into doing it. Thanks for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question