c# - 如何调整 "is a type but is used like a variable"?

标签 c# .net asp.net generics

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

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

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

如何调整上述误差?

最佳答案

问题出在线路上

List<Customer> li = List<Customer>();

你需要加上"new"

List<Customer> li = new List<Customer>();

此外,下一行应该是:

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

关于c# - 如何调整 "is a type but is used like a variable"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/934479/

相关文章:

c# - 计算点击点的角度

c# - 如何从 .net 错误对话框发送错误报告?

c# - 为什么从类派生会导致有关父构造函数的错误?

html - 页面的 Css 影响它的布局

javascript - 如何在用户控件中的aspx页面上调用外部javascript方法

c# - 使用 WPF C# 中的多个控件组合创建自定义控件

c# - 拒绝访问 C :\program file\myapplicationfolder\

javascript - window.print() 在每一页上重复模态

c# - Asp.net grdiview : can i format the dataitems in an itemtemplate?

C# 嵌套 null 检查 if 语句