如何调整“是一种类型,但像变量一样使用”?

我正在尝试在Web服务中生成一些代码。 但是它返回了2个错误:

1)List是一种类型,但是像变量一样使用

2)方法“客户”没有超载需要“3个参数”

[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] public class wstest : System.Web.Services.WebService { [WebMethod] public List GetList() { List li = List(); li.Add(new Customer("yusuf", "karatoprak", "123456")); return li; } } public class Customer { private string name; private string surname; private string number; public string Name { get { return name; } set { name = value; } } public string SurName { get { return surname; } set { surname = value; } } public string Number { get { return number; } set { number = value; } } } 

我如何调整以上错误?

问题在于此

 List li = List(); 

你需要添加“新”

 List li = new List(); 

另外,下一行应该是:

 li.Add(new Customer{Name="yusuf", SurName="karatoprak", Number="123456"}); 

编辑:如果您使用的是VS2005,那么您必须创建一个带有3个参数的新构造函数。

 public Customer(string name, string surname, string number) { this.name = name; this.surname = surname; this.number = number; } 

这个

 List li = List(); 

需要是:

 List li = new List(); 

并且您需要创建一个Customer构造函数,该构造函数接受您想要传递的3个参数。 默认的Customer构造函数不带参数。

回答你的第二个问题:

您需要创建一个带有三个参数的构造函数:

 public Customer(string a_name, string a_surname, string a_number) { Name = a_name; SurName = a_surname; Number = a_number; } 

或者在创建对象后设置值:

 Customer customer = new Customer(); customer.Name = "yusuf"; customer.SurName = "karatoprak"; customer.Number = "123456"; li.Add(customer); 

由于Customer类中的所有属性都具有公共setter,因此您不必创建构造函数(正如大多数人所建议的那样)。 您还可以使用默认的无参数构造函数并设置对象的属性:

 Customer c = new Customer(); c.Name = "yusuf"; c.SurName = "karatoprak"; c.Number = "123456"; li.Add(c);