【问题标题】:How do I pass arguments to a public class in C#?如何将参数传递给 C# 中的公共类?
【发布时间】:2012-02-24 09:19:02
【问题描述】:

如何将参数传递给 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";

但这失败了。

【问题讨论】:

  • 您是否在 DoSomething 类中声明了“apple”变量?
  • 您使用的方法public static void Main(System.String[] args)通常用于接受命令行参数。
  • 为了帮助您移动谷歌搜索,您希望在类上设置属性值,而不是向类传递参数(在构造函数之外,这没有意义 - 类方法获取提供的变量,而不是类本身)。
  • 如果你想使用类而不是取出主方法并为苹果橙香蕉等创建属性..
  • @mmcglynn:旁注 - 您不会在 C# 中“将参数传递给类”(甚至不确定可以使用哪种语言),而是“将参数传递给方法”,其中 method 是某个类的静态方法,如 DoSomething.Main("string_arg") 或只是类 myInstance.Method(42) 的成员。

标签: c# .net arguments console-application


【解决方案1】:

首先,让我们为您的版本添加注释,然后继续您可能想要的内容。

// 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" 结束

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

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

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

【讨论】:

  • 感谢您展示参数示例。它与 C++ 完全不同。
【解决方案2】:

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

【讨论】:

  • 这行得通。没关系,这是一个用于控制台的类。我正在以有限的方式将其重新用于网页,因此感谢所有帮助。
【解决方案3】:

使用公共属性,您可以使用auto-implemented property 开头:

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

【讨论】:

    【解决方案4】:

    构造函数:

    public class DoSomething
    {
        public DoSomething(String mystring) { ... }
    
        static void Main(String[] args) {
            new DoSomething(args[0]);
        }
    }
    

    编辑

    注意到 C# 在线书籍是德语的。但我敢肯定也有英文书。

    【讨论】:

      【解决方案5】:

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

      您可以通过使它们成为类的成员来访问它们,如下所示:

      我原来的代码 sn-p 错了;您的 Main 方法是静态的,因此您无法访问实例变量。

      public class DoSomething 
      { 
          public string apple;
      
          public void Main(System.String[] args) 
          { 
               apple = args[0];
          } 
      } 
      

      【讨论】:

      • 如果没有实例,你就不能......编辑:实际上你的代码现在是错误的......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 2018-09-30
      • 2021-09-11
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多