c# visual studio how to pass value from subclass to parent class? -
i'm new here, , new in c# programming in visual studio.
currently have assignment c# refactoring.
this original class
calculator.cs
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; namespace huang_kai_30077528_assign1 { public partial class calculator : form { public calculator() { initializecomponent(); } private void radiobutton1_checkedchanged(object sender, eventargs e) { } private void caloriecalculation() { if (rbtnmale.checked) { txtcalories.text = (66 + (6.3 * convert.todouble(txtweight.text)) + (12.9 * ((convert.todouble(txtfeet.text) * 12) + convert.todouble(txtinches.text))) - (6.8 * convert.todouble(txtage.text))).tostring(); } else { txtcalories.text = (655 + (4.3 * convert.todouble(txtweight.text)) + (4.7 * ((convert.todouble(txtfeet.text) * 12) + convert.todouble(txtinches.text))) - (4.7 * convert.todouble(txtage.text))).tostring(); } } private void weightcalculation() { double malevariable = 50; double femalevariable = 45.5; double formula = (2.3 * (((convert.todouble(txtfeet.text) - 5) * 12) + convert.todouble(txtinches.text))); if (rbtnmale.checked) { txtidealweight.text = ((malevariable + formula) * 2.2046).tostring(); } else { txtidealweight.text = ((femalevariable + formula) * 2.2046).tostring(); } } private void txtfeetvalidation() { double feet; if (!double.tryparse(txtfeet.text, out feet)) { messagebox.show("feet must numeric value."); txtfeet.select(); if (!(convert.todouble(txtfeet.text) >= 5)) { messagebox.show("height has equal or greater 5 feet!"); txtfeet.select(); return; } } } private void txtinchesvalidation() { double inches; if (!double.tryparse(txtinches.text, out inches)) { messagebox.show("inches must numeric value."); txtinches.select(); return; } } private void txtweightvalidation() { double weight; if (!double.tryparse(txtweight.text, out weight)) { messagebox.show("weight must numeric value."); txtweight.select(); return; } } private void txtagevalication() { double age; if (!double.tryparse(txtage.text, out age)) { messagebox.show("age must numeric value."); txtage.select(); return; } } private void btncalculate_click(object sender, eventargs e) { txtfeetvalidation(); txtinchesvalidation(); txtweightvalidation(); txtagevalication(); //clear old results txtidealweight.text = ""; txtcalories.text = ""; caloriecalculation(); weightcalculation(); } private void label8_click(object sender, eventargs e) { } private void btnexit_click(object sender, eventargs e) { application.exit(); } private void form1_load(object sender, eventargs e) { } private void btnaddpatient_click(object sender, eventargs e) { if (!string.isnullorempty(txtidealweight.text)) { form secondform = new patientinfo(this); secondform.show(); } else { messagebox.show("please enter datas , click on 'add patient' add patient's records"); } } private void btnpatientlist_click(object sender, eventargs e) { form secondform = new patientlist(this); secondform.show(); } private void btnclear_click(object sender, eventargs e) { rbtnmale.checked = false; rbtnfemale.checked = false; txtfeet.text = ""; txtinches.text = ""; txtweight.text = ""; txtage.text = ""; txtcalories.text = ""; txtidealweight.text = ""; } private void groupbox1_enter(object sender, eventargs e) { } } }
the following classed parent class , sub-classes setup.
parent: calculator.cs
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; namespace huang_kai_30077528_assign1 { public partial class calculator : form { //private string malecaloriescalculation(); //private string maleweightcalculation(); //private string femalecaloriescalculation(); //private string femaleweightcalculation(); public calculator() { initializecomponent(); } private void rbtnmale_checkedchanged(object sender, eventargs e) { } private void btncalculate_click(object sender, eventargs e) { //clear old results txtidealweight.text = ""; txtcalories.text = ""; /* validate user input: */ //validate height (feet) numeric value double result; if (!double.tryparse(txtfeet.text, out result)) { messagebox.show("feet must numeric value."); txtfeet.select(); return; } //validate height (inches) numeric value if (!double.tryparse(txtinches.text, out result)) { messagebox.show("inches must numeric value."); txtinches.select(); return; } //validate weight numeric value if (!double.tryparse(txtweight.text, out result)) { messagebox.show("weight must numeric value."); txtweight.select(); return; } //validate age numeric value if (!double.tryparse(txtage.text, out result)) { messagebox.show("age must numeric value."); txtage.select(); return; } if (!(convert.todouble(txtfeet.text) >= 5)) { messagebox.show("height has equal or greater 5 feet!"); txtfeet.select(); return; } /*end validation*/ calculation(); } private void calculation() { if (rbtnmale.checked) { txtcalories.text = malecalculator.malecalories().tostring(); //txtcalories.text = malecalculator.malecaloriescalculation(); //txtidealweight.text = malecalculator.maleweightcalculation(); } else { txtcalories.text = femalecalculator.femalecaloriescalculation(); txtidealweight.text = femalecalculator.femaleweightcalculation(); } } private void btnclear_click(object sender, eventargs e) { rbtnmale.checked = false; rbtnfemale.checked = false; txtfeet.text = ""; txtinches.text = ""; txtweight.text = ""; txtage.text = ""; txtcalories.text = ""; txtidealweight.text = ""; } } }
subclass: malecalculation.cs
using system; using system.collections.generic; using system.linq; using system.text; namespace huang_kai_30077528_assign1 { class malecalculator: calculator { //private string malecaloriescalculation; //private string maleweightcalculation; public malecalculator malecalories() { (66 + (6.3 * convert.todouble(txtweight.text)) + (12.9 * ((convert.todouble(txtfeet.text) * 12) + convert.todouble(txtinches.text))) - (6.8 * convert.todouble(txtage.text))).tostring(); return malecalories(); //this.txtcalories.text = new malecalculator.malecalories; } public malecalculator maleweight() { ((50 + (2.3 * (((convert.todouble(txtfeet.text) - 5) * 12) + convert.todouble(txtinches.text)))) * 2.2046).tostring(); return maleweight(); } } }
subclass: femalecalculation.cs
using system; using system.collections.generic; using system.linq; using system.text; namespace huang_kai_30077528_assign1 { class femalecalculator : calculator { public double femalecaloriescalculation() { txtcalories.text = (655 + (4.3 * convert.todouble(txtweight.text)) + (4.7 * ((convert.todouble(txtfeet.text) * 12) + convert.todouble(txtinches.text))) - (4.7 * convert.todouble(txtage.text))).tostring(); return femalecaloriescalculation(); } public double femaleweightcalculation() { txtidealweight.text = ((45.5 + (2.3 * (((convert.todouble(txtfeet.text) - 5) * 12) + convert.todouble(txtinches.text)))) * 2.2046).tostring(); return femaleweightcalculation(); } } }
as can see, these 2 sub-classes use calculation calories , ideal weight. plan take place of private void caloriecalculation() , private void weightcalculation() in original class calculator.cs.
the function need this:
when execute program , need calculate idealweight , calories, parent class calculator.cs result formula contain in sub-class, , tostring in text box. that's why there txtcalories , txtidealweight inside calculator.cs.
so question how results in sub-class, , fill in text boxes?
guys, please me important me!!
thank all!!
do mean virtual function? if so,
class ancestor { public virtual int dosomething() { // execute commands here. // throw exception. throw new notimplementedexception(); } } class derived_a : ancestor { public override int dosomething() { return 1 + 1; } } class derived_b : ancestor { public override int dosomething() { return 1 + 2; } }
this ancestry, virtual functions. more on this:
- practical usage of virtual functions in c#
- http://msdn.microsoft.com/en-us/library/9fkccyh4(v=vs.80).aspx
this type of code can done interfaces. see http://msdn.microsoft.com/en-us/library/vstudio/ms173156.aspx.
Comments
Post a Comment