Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中向公共类传递参数?_C#_.net_Arguments_Console Application - Fatal编程技术网

如何在C#中向公共类传递参数?

如何在C#中向公共类传递参数?,c#,.net,arguments,console-application,C#,.net,Arguments,Console Application,如何在C#中向公共类传递参数。 我是C#的新手,所以请原谅n00b问题 给定此示例类: 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];

如何在C#中向公共类传递参数。 我是C#的新手,所以请原谅n00b问题

给定此示例类:

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
    }
}
如何传递请求的参数

我希望能写一些类似的东西:

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

但此操作失败。

使用公共属性,您可以使用开始:

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

在您提供的示例中,您正在创建的变量的范围是
Main
方法;它们不是类级别的变量

public class DoSomething 
{ 
    public string apple;

    public void Main(System.String[] args) 
    { 
         apple = args[0];
    } 
} 
您可以通过使它们成为类的成员来访问它们,如下所示:

我原来的代码片段是错误的;您的
Main
方法是静态的,因此无法访问实例变量

public class DoSomething 
{ 
    public string apple;

    public void Main(System.String[] args) 
    { 
         apple = args[0];
    } 
} 
建造商:

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

    static void Main(String[] args) {
        new DoSomething(args[0]);
    }
}
编辑
注意到C#在线图书是德语的。但我肯定也有英文书。

首先,让我们用笔记敲打你的版本,然后继续讨论你可能想要的

// 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()
}
下面是您可能想要的类定义:

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;
    }

}
然后您可以创建/操作类,如下所示。在每种情况下,实例都将以Apple=“foo”和Orange=“bar”结束


通过命令行启动应用程序时,将填充
Main
方法的
String[]args
参数:

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

如果要以编程方式传递这些参数,必须将变量设置为公共属性,例如:

public class DoSomething
{
   public string Apple { get; set; }
   public string Orange { get; set; }
   public string Banana { get; set; }
   // other fruits...
}
然后你可以做:

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

        // do something
    }
}

您是否在DoSomething类中声明了一个“apple”变量?您使用的方法
public static void Main(System.String[]args)
通常用于接受命令行参数。为了帮助您移动谷歌搜索,您需要在类上设置属性值,而不是传递类参数(这在构造函数之外是没有意义的——类方法得到的是提供的变量,而不是类本身)。如果你想使用类,那么就拿出main方法并为apple orange banana等创建属性。@mmcglyn:旁注——你不能在C#中“将参数传递给类”(甚至不确定用什么语言可以这样做),而是“将参数传递给方法”,其中方法要么是某个类(如
DoSomething.Main(“string_arg”)
的静态方法,要么只是类
myInstance.method(42)的成员。如果没有实例,你不能。编辑:实际上你的代码现在是错误的。这是正确的。这是一个想要控制台的类。我用网页的方式限制它,所以我很感激所有的帮助。谢谢你展示了一个参数例子。它的方式与C++不同。