Solved

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

Posted on 2014-10-24
2
477 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
2 Comments
 
LVL 35

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now