C# 如何调整“是一种类型但像变量一样使用”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/934479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to adjust "is a type but is used like a variable"?
提问by Penguen
I'm trying to generate some code in a web service. But it's returning 2 errors:
我正在尝试在 Web 服务中生成一些代码。但它返回 2 个错误:
1) List is a type but is used like a variable
1) List 是一种类型,但像变量一样使用
2) No overload for method 'Customer' takes '3 arguments'
2) 方法“Customer”没有重载需要“3 个参数”
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class wstest : System.Web.Services.WebService
{
[WebMethod]
public List<Customer> GetList()
{
List<Customer> li = List<Customer>();
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; } }
}
How can i adjust above error?
我如何调整上述错误?
采纳答案by Jose Basilio
The problem is at the line
问题出在线路上
List<Customer> li = List<Customer>();
you need to add "new"
你需要添加“新”
List<Customer> li = new List<Customer>();
Additionally for the next line should be:
另外下一行应该是:
li.Add(new Customer{Name="yusuf", SurName="karatoprak", Number="123456"});
EDIT:If you are using VS2005, then you have to create a new constructor that takes the 3 parameters.
编辑:如果您使用的是 VS2005,那么您必须创建一个带有 3 个参数的新构造函数。
public Customer(string name, string surname, string number)
{
this.name = name;
this.surname = surname;
this.number = number;
}
回答by Mark Sherretta
This
这个
List<Customer> li = List<Customer>();
needs to be:
需要是:
List<Customer> li = new List<Customer>();
and you need to create a Customer constructor which takes the 3 arguments you want to pass. The default Customer constructor takes no arguments.
并且您需要创建一个 Customer 构造函数,它接受您要传递的 3 个参数。默认的 Customer 构造函数不接受任何参数。
回答by ChrisF
To answer your second question:
回答你的第二个问题:
You either need to create a constructor that takes the three arguments:
您要么需要创建一个带有三个参数的构造函数:
public Customer(string a_name, string a_surname, string a_number)
{
Name = a_name;
SurName = a_surname;
Number = a_number;
}
or set the values after the object has been created:
或在创建对象后设置值:
Customer customer = new Customer();
customer.Name = "yusuf";
customer.SurName = "karatoprak";
customer.Number = "123456";
li.Add(customer);
回答by Guffa
As all the properties in the Customer
class has public setters, you don't absolutely haveto create a constructor (as most have suggested). You also have the alternative to use the default parameterless constructor and set the properties of the object:
正如所有属性Customer
类具有公共setter方法,你不绝对有建立一个构造函数(因为大多数建议)。您还可以选择使用默认的无参数构造函数并设置对象的属性:
Customer c = new Customer();
c.Name = "yusuf";
c.SurName = "karatoprak";
c.Number = "123456";
li.Add(c);