?
Solved

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

Posted on 2014-10-24
2
Medium Priority
?
513 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 2000 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

Technology Partners: 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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression
Suggested Courses

752 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