c# - Not using method in derived class -


this question has answer here:

uni assignment requires build pizza app in c# derives sauce ingredient. have list<ingredient> storing objects of both types.

pizza.cs calls method getinstructions() different in derived method. unfortunately sauce.getinstructions() never being called. can see, there's debug line in there should pop message box when program executing routine it's not popping up. can suggest why not?

pizza.cs contains following method:

public string buildinstructionlist()     {   // iterate through list of selected toppings , build single string display in gui     int count = 1; string instructions = "";     foreach (var in toppings)         {         instructions = instructions += string.format("step {0}: {1}", count, i.getinstructions());         count++;         }         return instructions;     } 

ingredient.cs contains:

public virtual string getinstructions()     {         string instructionline;         string qty = this.getingredientquantity().tostring();         string unit = this.getingredientunit();         string name = this.getingredientname();         instructionline = string.format("add {0} {1} of {2} pizza.\n", qty, unit, name);         return instructionline;     } 

sauce.cs contains:

public new string getinstructions()     {         pizzagui.message("i here!");         string instructionline;         string qty = this.getingredientquantity().tostring();         string unit = this.getingredientunit();         string name = this.getingredientname();         instructionline = string.format("apply {0} {1} of {2} pizza.\n", qty, unit, name);         return instructionline;     } 

you need use override in place new in sauce.cs method.

from msdn override modifier extends base class method, , new modifier hides it.

public override string getinstructions()     {         pizzagui.message("i here!");         string instructionline;         string qty = this.getingredientquantity().tostring();         string unit = this.getingredientunit();         string name = this.getingredientname();         instructionline = string.format("apply {0} {1} of {2} pizza.\n", qty, unit, name);         return instructionline;     } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -