Solved

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

Posted on 2014-10-24
2
468 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 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

12 Experts available now in Live!

Get 1:1 Help Now