This is the first time I work with lists, and it seems I donβt understand. I have a Customer class with a list of clients as a property in the Customer class (can this be done like this?)
public class Customer
{
private List<Customer> customers = new List<Customer>();
public List<Customer> Customers
{
get { return customers; }
set { customers = value; }
}
In my program, I add to this list of clients, like this:
Customer C = new Customer();
Customer.InputCustomer(C);
C.Customers.Add(C);
Now I need to show customers on this list. I added the AllCustomers function to the client class as follows:
public static void AllCustomers()
{
foreach (Customer customer in Customers)
{
Console.WriteLine("Customer ID: " + customer.ID);
Console.WriteLine("Customer Name: " + customer.FullName);
Console.WriteLine("Customer Address: " + customer.Address);
Console.WriteLine();
}
}
But I get this error in the foreach statement:
An object reference is required for a non-static field, method or property 'AddCustomerList.Customer.Customers.get'
Like I said, this is the first time I use List, maby I donβt get it right? Can anyone help me please!
source
share