C# Class scope issue? -
i student. our first project using 2 different class files, , second 1 using forms. so, don't have experience either.
the object declared , instantiated, object not recognized in of subsequent methods. following code on book. code on book declares/instantiates this:
new order = new order( );
i think needs this, still doesn't subsequent references:
order neworder = new order( );
this a complete listing of driver , 2 class files:
driver:
using system; using system.collections.generic; using system.linq; using system.threading.tasks; using system.windows.forms; namespace diner { static class program { /// <summary> /// main entry point application. /// </summary> [stathread] static void main() { application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); application.run(new ordergui()); } } }
ordergui.cs:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace diner { public partial class ordergui : form { public ordergui() { initializecomponent(); } //for load - create object private void ordergui_load(object sender, system.eventargs e) { neworder = new order(); (int = 0; < neworder.menuentree.length; i++) { this.lstbxentree.items.add(neworder.menuentree[i]); } } // event handler gets entree // listbox , sets entree price of order object. private void lstbxentree_selectedindexchanged (object sender, system.eventargs e) { neworder.entree = this.lstbxentree.text; } // event handler gets special request - // if 1 selected predefined list. private void cmbospecial_selectedindexchanged (object sender, system.eventargs e) { neworder.specialrequest = this.cmbospecial.text; } // menu item displays order. private void menudisplayorder_click(object sender, system.eventargs e) { } // event handler gets radio button selected , // sets drink selection order object private void drink_checkedchanged(object sender, system.eventargs e) { if (this.radtea.checked) neworder.drinkselection = radtea.text; else if (this.radcoffee.checked) neworder.drinkselection = radcoffee.text; else if (this.radsoda.checked) neworder.drinkselection = radsoda.text; else if (this.radlemon.checked) neworder.drinkselection = radlemon.text; else if (this.radjuice.checked) neworder.drinkselection = radjuice.text; else if (this.radmilk.checked) neworder.drinkselection = radmilk.text; } // event handler gets raised when check box // water gets clicked. private void ckbxwater_checkedchanged (object sender, system.eventargs e) { if (this.ckbxwater.checked) neworder.waterselection = true; else neworder.waterselection = false; } // event handler gets raised when user types // values text area of combo box. private void cmbospecial_keypress (object sender, system.windows.forms.keypresseventargs e) { neworder.specialrequest = this.cmbospecial.text; } // event handler gets raised when edit menu // clicked change entree. private void menueditentree_click (object sender, system.eventargs e) { } // event handler gets raised when edit menu // clicked change drink. private void menueditdrink_click (object sender, system.eventargs e) { } // clears selections drink radio buttons. public void cleardrinks() { this.radmilk.checked = false; this.radjuice.checked = false; this.radsoda.checked = false; this.radlemon.checked = false; this.radtea.checked = false; this.radcoffee.checked = false; } // clears selections new order // can placed. resets order object // default values. private void menuclearorder_click (object sender, system.eventargs e) { } // displays values current instance of // order object members private void menuplaceorder_click(object sender, system.eventargs e) { } // event handler gets raised when edit menu // clicked change special requests. private void menueditspecial_click(object sender, system.eventargs e) { } // event handler gets raised when // menu clicked show message. private void menuabout_click(object sender, system.eventargs e) { messagebox.show("student union -" + " diner valley" + "\n\n\nversion 1.0"); } // event handler gets raised when // exit clicked private void menuexit_click(object sender, system.eventargs e) { application.exit(); } } }
order.cs: using system; using system.windows.forms;
namespace diner { public class order { public string[] menuentree = new string[] {"chicken salad", "ham , cheese", "turkey", "vegetable wrap", "tuna salad", "avocado , cheese", "club", "peanut butter & jelly", "cheese toasty", "reuben"}; public decimal[] menuentreeprice = new decimal[] {4.50m, 5.00m, 4.75m, 4.00m, 4.50m, 4.00m, 5.50m, 3.75m, 3.50m, 5.00m}; private string entree; private bool waterselection; private string drinkselection; private string specialrequest; private decimal entreeprice; private decimal drinkprice; // default constructor public order() { entree = ""; waterselection = false; specialrequest = ""; drinkprice = 0; entreeprice = 0; } //property entree public string entree { { return entree; } set { entree = value; setentreeprice(); } } // property special request public string specialrequest { { return specialrequest; } set { specialrequest = value; } } // property water selection public bool waterselection { set { waterselection = value; } } // property drink selection public string drinkselection { { return drinkselection; } set { drinkselection = value; setdrinkprice(); } } // read-only property entreee price public decimal entreeprice { { return entreeprice; } } // read-only property drink price public decimal drinkprice { { return drinkprice; } } // after entree set, store entree price public void setentreeprice() { (int = 0; < menuentree.length; i++) { if (menuentree[i] == entree) { entreeprice = menuentreeprice[i]; } } } // return water selection public string getwaterselection() { string waterornot; if (waterselection) { waterornot = "water"; } else { waterornot = "no water"; } return waterornot; } // after drink set, store drink price public void setdrinkprice() { switch (drinkselection) { case "tea": case "coffee": drinkprice = 1.50m; break; case "soda": case "lemonade": drinkprice = 2.00m; break; case "milk": case "juice": drinkprice = 1.75m; break; } } // return total cost of order public decimal determinetotalcharges() { return entreeprice + drinkprice; } public override string tostring() { return "toatal due: " + determinetotalcharges().tostring("c"); } } }
Comments
Post a Comment