how to adjust “is a type but is used like a variable”?

I’m trying to generate some code in a web service. But it’s returning 2 errors:

1) List is a type but is used like a variable

2) No overload for method ‘Customer’ takes ‘3 arguments’

[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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

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

Method 2

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.

Method 3

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

Method 4

As all the properties in the Customer class has public setters, you don’t absolutely have to 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 c = new Customer();
c.Name = "yusuf";
c.SurName = "karatoprak";
c.Number = "123456";
li.Add(c);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x