Click here to Skip to main content
15,884,810 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
namespace ShopTime
{
    class Product
    {
        public int ProductID { get; set; }
        public string ProductCode { get; set; }
        public string ProductName { get; set; }
        public double Price { get; set; }

        public  Product()
        {

        }

        public Product(int productID,string protuctCode,string productName, double price)
        {
            this.ProductID = productID;
            this.ProductCode = protuctCode;
            this.ProductName = productName;
            this.Price = price;
        }

        public double Set_KDV()
        {
            return this.Price * 1.18;
        }
    }
public enum Brands
    {
        Xiaomi = 1,
        Apple = 2,
        Samsung = 3,
        Oppo = 4,
        Asus = 5
    }

    class Cell_Phone : Product
    {

        public Brands Brand { get; set; }

        public Cell_Phone(int productID,double price, Brands brand)
        {
            switch (brand)
            {
                case Brands.Xiaomi:
                    this.ProductID = 3300;
                    this.Price = 3000.0;
                    this.Brand = Brands.Xiaomi;
                    break;
                case Brands.Apple:
                    this.ProductID = 3389;
                    this.Price = 5000.0;
                    this.Brand = Brands.Apple;
                    break;
                case Brands.Samsung:
                    this.ProductID = 1389;
                    this.Price = 3000.0;
                    this.Brand = Brands.Samsung;
                    break;
                case Brands.Oppo:
                    this.ProductID = 3381;
                    this.Price = 3000.0;
                    this.Brand = Brands.Oppo;
                    break;
                case Brands.Asus:
                    this.ProductID = 1389;
                    this.Price = 3000.0;
                    this.Brand = Brands.Asus;
                    break;
                default:
                    break;
            }
        }

        public Cell_Phone()
        {
        }   
    }
class Program
    {
        static void Main(string[] args)
        {
            Basket basket = new Basket();

            Cell_Phone phone = new Cell_Phone(Brands.Xiaomi); // There is error.
        }
    }
}


What I have tried:

I deleted and retyped. I'm sure I didn't make a mistake.
Posted
Updated 2-Aug-21 21:47pm

1 solution

It is because you created your Cell_Phone class with a constructor requiring three parameters but only pass one when the object is instantiated in Main class you only pass a brands enum.

As the constructor seems to populate the fields depending on the value of the enum you could probably get rid of the in productID and price parameters in the Cell_Phone constructor.
public Cell_Phone(int productID, double price, Brands brand)

Cell_Phone phone = new Cell_Phone(Brands.Xiaomi);

As follows

public Cell_Phone(Brands brand)

Cell_Phone phone = new Cell_Phone(Brands.Xiaomi);
 
Share this answer
 
v2
Comments
Tony Hill 3-Aug-21 3:43am    
No you are not, when you instantiate the Cell_Phone object in the Main method you only pass the enum to the constructor into the constructor of the Cell_Phone object.

Cell_Phone phone = new Cell_Phone(Brands.Xiaomi);

The signature for the Cell_phone constructor expects three parameters

public Cell_Phone(int productID, double price, Brands brand)

So your options are to pass more parameters when the Cell_Phone object is created or as I suggested alter Cell_Phone constructor to accept just the enum, as I mentioned before the variable ProductID and Price are set in the constructor dependent on the value of the enum used when the Cell_Phone object is created so why do you need to pass them into the constructor.

Becaus the const

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900