【问题标题】:Using String Array As Object Argument使用字符串数组作为对象参数
【发布时间】:2015-07-15 23:06:50
【问题描述】:

所以我有一个具有字符串数组参数的类。我想要做的是将多个字符串存储到这个属于这个类的数组中。代码如下所示:

//Class part of it. Class is called "Event"
public class Event
{
   public string[] seats = new string [75];

   public Event(string[] seats)
   {
      this.seats = seats;
   }
}

// the main code that uses "Event" Class

string[] seatnumber = new string[75];
Event show = new Event (seatnumber[]); //And that is where the error comes in. 

任何帮助将不胜感激!

【问题讨论】:

  • 在将座位号放入 Event 构造函数时,去掉括号内的括号。
  • 括号是类型声明、构造和元素检索的一部分。你需要传递引用变量,它的名字是seatnumber

标签: c# arrays class arguments


【解决方案1】:

调用数组时,变量名不需要括号[]

Event show = new Event(seatnumber);

它与您之前在代码中调用的相同:

this.seats = seats;

seats 也是一个数组,尽管您在调用它时没有添加 [] - 所以没有错误。

【讨论】:

    【解决方案2】:

    在将座位号放入 Event 构造函数时,去掉方括号。

    For reference.

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 1970-01-01
      • 2021-08-16
      • 2019-04-04
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      相关资源
      最近更新 更多