0

Trying to create a list of customers that can increase to any size for a salesperson. I'm new to OOP in C# and I'm not sure what I'm doing wrong here. Is it because I am trying to add derived objects to my list of a base class? If so what would be the alternative?

class Program
    {
        List<Customer> customers = new List<Customer>();

        static void Main(string[] args)
        {
            GetCustomerInfo();
            
        }
        static void GetCustomerInfo()
        {
            int prod1, prod2;
            Console.WriteLine("Please enter the following info.");
            Console.WriteLine("Type '1' for pickup, '2' for delivery: ");
            string option = Console.ReadLine();
            Console.Write("Name: ");
            string name = Console.ReadLine();
            Console.Write("10 digit Phone Number: ");
            string number = Console.ReadLine();
            Console.Write("Quantity of product 1: ");
            bool success = false;
            while (!success)
            {
                success = int.TryParse(Console.ReadLine(), out prod1);
            }
            bool success2 = false;
            while (!success2)
            {
                success2 = int.TryParse(Console.ReadLine(), out prod2);
            }
            if(option == "1")
            {
                Console.WriteLine("Address: ");
                string address = Console.ReadLine();
                customers.Add(new DeliveryCustomer() { Name = name, PhoneNumber = number, Address = address });
            }
            else if (option == "2")
            {
                Console.WriteLine("Pick up time: ");
                string time = Console.ReadLine();
                customers.Add(new PickUpCustomer() { Name = name, PhoneNumber = number, PickUpTime = time });
            }
            

customers.Add(new PickUpCustomer() { Name = name, PhoneNumber = number, PickUpTime = time });

Gives the following error. An object reference is required for the nonstatic field, method, or property 'member

        }

    }

class Customer
    {
        static int productOnePrice = 5;
        static int productTwoPrice = 10;
        private string name;
        private string phoneNumber;             
        private int quantityOfProductOne;
        private int quantityOfProductTwo;

        public Customer()
        {
            name = "";
            phoneNumber = "";
            quantityOfProductOne = 0;
            quantityOfProductTwo = 0;
        }
        public string Name 
        {
            get => name;
            set => name = value;
        }

        public string PhoneNumber
        {
            get => phoneNumber;
            set
            {
                if(value.Length != 10)
                {
                    Console.WriteLine("Invalid number. Must be 10 digits.");
                }
                else
                {
                    phoneNumber = value;
                }
            }
        }

        public int QuantityOfProductOne
        {
            get => quantityOfProductOne;
            set => quantityOfProductOne = value;

        }

        public int QuantityOfProductTwo
        {
            get => quantityOfProductTwo;
            set => quantityOfProductTwo = value;

        }
    
        }
class DeliveryCustomer : Customer
{
    private string address;

    public string Address
    {
        get => address;
        set => address = value;
    }
        
    public DeliveryCustomer()
    {
        address = "";
        }
    
    }
class PickUpCustomer : Customer
{
    private string pickUpTime;

    public string PickUpTime
    {
        get => pickUpTime;
        set => pickUpTime = value;
    }

    public PickUpCustomer()
    {
        pickUpTime = "";
    }
}

I apologize if this is a silly question, I just haven't been able to wrap my head around some of the OOP concepts.

Kyle Close
  • 29
  • 3
  • If you wanted to just show some information to answer the question in the title by providing `List customers = new List();` with supporting code SO question is not the right place to do so. In some cases providing answer yourself (as separate answer, not inside the question) is ok. If you have some other question than asked in the title of the post - please [edit] the question to clarify. – Alexei Levenkov Oct 09 '21 at 02:10
  • What exactly is not working? What result are you seeing? You say that you are trying to create a list? Is the list not being created? Are you not able to add Customers to your list? Are you getting an error? If so, where and what is the error? Please add more details to your question. – David.Warwick Oct 09 '21 at 02:15
  • Sorry I should have been more specific. When I try to do `customers.Add(new Customer() { Name = name, PhoneNumber = number, PickUpTime = time }) ;` I get the following error "An object reference is required for the nonstatic field, method, or property 'member'' – Kyle Close Oct 09 '21 at 02:26
  • This question answers your question: See https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop – David.Warwick Oct 09 '21 at 02:36

1 Answers1

0

GetCustomerInfo() is a static method, while List<Customer> customers is an instance field, you may not access an instance field from a static method, so hopefully making customers a static field should solve the problem

h8moss
  • 4,626
  • 2
  • 9
  • 25