Ali
Ali

Reputation: 17

Lists not instantiating C#

Just started learning how to code. I am making a Ordering System with three objects Customer, Inventory, and OrderForm. I have to be able to add/save, Customer information to a .csv file. via StreamWriter. Also be able to edit and delete and specific information anytime I want.

Now from what I have researched it seems Lists are the best way to go about holding the information till they have been written into the file, also they are much easier to edit and delete the information.

I have a class called Customer, where I have put all the properties for a customer information (name, ID, state, etc.) and their get and set properties.

I have a windows Form with textfields, and buttons to input the the above mentioned data from a customer and save it (via button)

In the windows form class

I've made a List to hold that information:

public List<Customer> CustInfo { get; set; }

but for some reason this is not working:

CustInfo = new List<Customer>();

the CustInfo is showing a red line, and I cannot do an .Add method to it.

any thoughts?

Upvotes: 0

Views: 450

Answers (2)

d.moncada
d.moncada

Reputation: 17402

You can also have the property instantiate it for you..some poeple dont like this method though since the property ends up checking for null each time you use it.

private List<Customer> _custInfo;
public List<Customer> CustInfo 
{ 
  get { return _custInfo ?? (_custInfo = new List<Customer>()); }
  set { _custInfo = value; }
}

Upvotes: 2

AD.Net
AD.Net

Reputation: 13409

public class YourClass
{
    public List<Customer> CustInfo { get; set; }
    public YourClass()
    {
        CustInfo = new List<Customer>();
    }
}

That should work, or do the instantiation inside a method.

Upvotes: 4

Related Questions