How to pass args command line parameter from Program.cs to Form1.cs ?

P

Pia Stevens

For a GUI CSharp program I could define command line parameter as well
by declaring them in Main():

static void Main(string[] args)

But how do I pass them easily to Form1 ?

Or should I declare them in Program.cs and access them from Form1.cs ?

Pia
 
Z

zacks

For a GUI CSharp program I could define command line parameter as well
by declaring them in Main():

static void Main(string[] args)

But how do I pass them easily to Form1 ?

Or should I declare them in Program.cs and access them from Form1.cs ?

As with most computer algorithms, the task can be accomplished in a
number of ways. You can declare global variables in the main program
and assign the command line arguments to them. Or you can make the
properties in the form class and initialize them before showing the
form.
 
D

Daniel Cigic

I would simple pass args to the constructor of the form .. like new
Form1(arg)
 
J

Jon Skeet [C# MVP]

Pia Stevens said:
For a GUI CSharp program I could define command line parameter as well
by declaring them in Main():

static void Main(string[] args)

But how do I pass them easily to Form1 ?

Or should I declare them in Program.cs and access them from Form1.cs ?

It's always worth thinking about forms and other GUI classes as just
like normal classes - because they are.

How would you pass parameters from one class to another normally? You
could use a constructor which takes parameters, or possibly have a
method or property on the class and provide them after construction.

These options are available for forms just as for any other class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top