Home > Software design >  How can I create a dynamic List of objects that can store multiple inherited/derived objects
How can I create a dynamic List of objects that can store multiple inherited/derived objects

Time:10-09

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.

CodePudding user response:

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

  • Related