0

I'm a new C# programmer, and am wondering why I'm getting a object does not contain a definition of 'methodName' and no extension method 'methodName' accepting a first argument of type 'object' could be found error. I'm not sure why it seems the two classes aren't connected.

Here is the class with my method definitions:

namespace Simple_Restaurant_Simulation
{
    class ChickenOrder
    {
        public int quantity;
        public int GetQuantity(int ChickenQuantity)
        {
            this.quantity = ChickenQuantity;
            return quantity;
        }

        public void CutUp()
        {
            return;
        }

        public void Cook()
        {
            return;
        }
    }
}

and the calling methods:

namespace Simple_Restaurant_Simulation
{
    class Employees
    {
        public dynamic NewRequest(int Quantity, string MenuItem)
        {
            if (MenuItem == "Chicken")
            {
                return new ChickenOrder();
            }

            else
            {
                return new EggOrder();
            }
        }

        public dynamic CopyRequest(dynamic MenuItem)
        {
            /*TODO: 
            if(existing order){
                return existing order;
            }           

            else { return "Whaddaya think I am, a miracle worker?"};

            */
            return null;
        }

        public int Inspect(object Order)
        {
            int InspectResult = 0;

            return InspectResult;
        }

        private string PrepareFood(object Order)
        {

            string PrepareResult = null;

            try
            {
                if (Order is ChickenOrder)
                {
                    for (int i=0; i < this.GetQuantity; i++)
                    {
                        Order.CutUp();
                    }
                    Order.Cook();
                    PrepareResult =  "Chicken order has been cooked";
                }
                                return PrepareResult;

            }
            catch (Exception)
            {
            }
        }
    }
}

Thanks for your help

4
  • 1
    What line is your error? Commented Apr 3, 2017 at 15:01
  • 6
    Hint: what's the compile-time type of the Order variable? The use of the is operator doesn't change that... Commented Apr 3, 2017 at 15:02
  • 1
    Does ChickenOrder even inherit from Order? Commented Apr 3, 2017 at 15:14
  • 3
    New programmers should not use dynamic or object. Dynamic is mainly for specific interop scenarios. Get a real compile-time type in there. Commented Apr 3, 2017 at 15:23

1 Answer 1

1

I am not sure what you are trying to do here but the reason why the PrepareMethod doesn't compile is that it accepts an object and the object type obviously has no CutUp() method.

You could use the as operator to try to cast the Order object to a ChickenOrder:

ChickenOrder chickenOrder = Order as ChickenOrder;
if (chickenOrder != null)
{
    for (int i = 0; i< this.GetQuantity; i++)
    {
        chickenOrder.CutUp();
    }
    chickenOrder.Cook();
    PrepareResult =  "Chicken order has been cooked";
}

Note that the cast will only succeed if the object that you are passing the PrepareFood method at runtime actually is a ChickenOrder. And for you to be able to pass a ChickenOrder object to a method that accepts an Order argument, the ChickenOrder class should inherit from Order:

class ChickenOrder : Order
{
    ...
}

Both EggOrder and ChickenOrder are specific types of orders that should inherit from the Order base class. This also means that you can change the return type of the NewRequest method from dynamic to Order and the type of the parameter that you are passing to the other methods from object to Order.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.