Search code examples
c#bindinglist

Fix implicitly typed error with multiple class calls


I am building a small inventory system for an assignment, and two of my calls to a class are returning an error indicating that there's an implicit assignment, but I'm not able to locate this implicit assignment.

The instructor has provided some classes that help with this assignment, and I am converting what he provided to work with my project; his code and mine are very similar, but his is not returning the errors.

Child class:

    class InHouse : Part
    {
        public int machineID;
        public InHouse() { }

        public InHouse(int ParID, string ParNa, double ParPrice, int ParInSto, int ParMaxSto, int ParMinSto, int MachID) : base(ParID, ParNa, ParPrice, ParInSto, ParMaxSto, ParMinSto)
        {
            machineID = MachID;
        }
    }

Abstract parent class:

    abstract class Part : Form 
    {
        public String PartName { get; set; }
        public int PartID { get; set; }
        public double price;
        public int inStock;
        public int maxStock;
        public int minStock;

        public int getPartID() { return PartID; }

        public string getPartName() { return PartName; }
        public double getPartPrice() { return price; }
        public int getPartInStock() { return inStock; }
        public int PartMaxStock() { return maxStock; }
        public int PartMinStock() { return minStock; }

        public Part() { }

        public Part(int partID, string partName, double partPrice, int partInStock, int partMaxStock, int partMinStock)
        {
            PartID = partID;
            PartName = partName;
            price = partPrice;
            inStock = partInStock;
            maxStock = partMaxStock;
            minStock = partMinStock;
        }

    }

Calling function inducing the error:

        private void AddPartSave_Click(object sender, EventArgs e)
        {
            if (isInHouse)
            {
                AllPartsList prt = new InHouse(Convert.ToInt32(ModifyPartIDBox.Text), ModifyPartNameBox.Text, Convert.ToDouble(ModifyPartPriceCostBox.Text), Convert.ToInt32(ModifyPartInventoryBox.Text), Convert.ToInt32(ModifyPartMaxInvBox.Text), Convert.ToInt32(ModifyPartMinBox.Text), Convert.ToInt32(PartMachineOrCompanyBox.Text));
            }

List class:

     class AllPartsList
    {
        private static BindingList<Part> partsList = new BindingList<Part>();
        public static BindingList<Part> PartsList { get { return partsList; } set { partsList = value; } }

        public static Part CurrentPart { get; set; }
        public static int CurrentPartID { get; set; }
        public static int CurrentPartIndex { get; set; }

        public static Part lookupPart(int i)
        {
            for (int j = 0; j < PartsList.Count; j++)
            {
                if (PartsList[j].PartID.Equals(i))
                {
                    CurrentPartIndex = j;
                    return PartsList[j];
                }
            }
            CurrentPartIndex = -1;
            return null;
        }
    }

Based on what I'm seeing in the instructor's code, this should work but the compiler error is

Cannot implicitly convert type 'InventorySystem.InHouse' to 'InventorySystem.AllPartsList'


Solution

  • You're declaring

    AllPartsList prt
    

    which means that prt is of type AllPartsList.

    But new Inhouse creates, well, a new object of type InHouse. You can't declare a variable of type AllPartsList and assign something entirely different to it.

    It would be like doing this:

    int x = new DateTime(2019,4,23);
    

    x is an int, so I can't assign a DateTime to it.