How do I pass arguments to a public class in C#?

30,615

Solution 1

The String[] args parameter of the Main method is populated when you launch the application via command line:

/your/application/path/DoSomething.exe arg1 arg2 arg3 ...

If you want to pass these arguments programmatically you have to set your variables as public Properties, so for example:

public class DoSomething
{
   public string Apple { get; set; }
   public string Orange { get; set; }
   public string Banana { get; set; }
   // other fruits...
}

Then you can do:

public class Test
{
    public static void  Main(System.String[] args)
    {
        DoSomething ds = new DoSomething();
        ds.Apple = "pie";

        // do something
    }
}

Solution 2

First, let's hit your version with notes, then move on to what you probably wanted.

// Here you declare your DoSomething class
public class DoSomething
{
    // now you're defining a static function called Main
    // This function isn't associated with any specific instance
    // of your class. You can invoke it just from the type,
    // like: DoSomething.Main(...)
    public static void Main(System.String[] args)
    {
        // Here, you declare some variables that are only in scope
        // during the Main function, and assign them values 
        System.String apple = args[0];
        System.String orange = args[1];
        System.String banana = args[2];
        System.String peach = args[3];
    }
        // at this point, the fruit variables are all out of scope - they
        // aren't members of your class, just variables in this function.

    // There are no variables out here in your class definition
    // There isn't a constructor for your class, so only the
    // default public one is available: DoSomething()
}

Here's what you probably wanted for your class definition:

public class DoSomething
{
    // The properties of the class.
    public string Apple; 
    public string Orange;

    // A constructor with no parameters
    public DoSomething()
    {
    }

    // A constructor that takes parameter to set the properties
    public DoSomething(string apple, string orange)
    {
        Apple = apple;
        Orange = orange;
    }

}

And then you could create / manipulate the class like the following. In each case, the instance will end up with Apple = "foo" and Orange = "bar"

DoSomething X = new DoSomething("foo", "bar");

DoSomething Y = new DoSomething();
Y.Apple = "foo";
Y.Orange = "bar";

DoSomething Z = new DoSomething()
{
    Apple = "foo",
    Orange = "bar"
};

Solution 3

Use a public property, you can use an auto-implemented property to start with:

public class DoSomething
{
   public string Apple {get;set;}
}

Solution 4

Constructor:

public class DoSomething
{
    public DoSomething(String mystring) { ... }

    static void Main(String[] args) {
        new DoSomething(args[0]);
    }
}

Edit

Noticed that the C# online book is in german language. But i'm sure that there are english books too.

Share:
30,615
mmcglynn
Author by

mmcglynn

Web Developer from Providence, RI. Doing my best to ask good questions. Hoping for civility and reason when I don't.

Updated on July 29, 2020

Comments

  • mmcglynn
    mmcglynn almost 4 years

    How do I pass arguments to a public class in C#. I am new to C#, so please forgive the n00b question.

    Given this sample class:

    public class DoSomething
    {
        public static void  Main(System.String[] args)
        {
    
            System.String apple = args[0];
            System.String orange = args[1];
            System.String banana = args[2];
            System.String peach = args[3];
    
            // do something
        }
    }
    

    How do I pass the requested arguments?

    I would expect to write something like:

    DoSomething ds = new DoSomething();
    ds.apple = "pie";
    

    But this fails.