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
Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
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 …

789 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