c# - How do I make a method private and access it from another class? -
i curious how can make addoperands method private rather public coded now. have read examples of , set accessors , still don't understand concept. how made addoperands method private in calcengine class , still able use method class?
using system; using system.collections.generic; using system.linq; using system.text; namespace project3_windowscalculator { class calcengine { private int operationresult; public int addoperands(int operand1, int operand2) { operationresult = operand1 + operand2; return operationresult; } public int subtractoperands(int operand1, int operand2) { operationresult = operand1 - operand2; return operationresult; } public int multiplyoperands(int operand1, int operand2) { operationresult = operand1 * operand2; return operationresult; } public int divideoperands(int operand1, int operand2) { operationresult = operand1 / operand2; return operationresult; } }
}
and here class uses it:
using system; using system.collections.generic; using system.linq; using system.text; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; namespace project3_windowscalculator { public partial class mainwindow : window { calcengine c = new calcengine(); bool addselected; bool subtractselected; bool multiplyselected; bool divideselected; bool operationselected; int operationresult; int operand1; int operand2; public mainwindow() { initializecomponent(); } private void c_click(object sender, routedeventargs e) { txtdisplay.text = "0"; operationresult = 0; addselected = false; subtractselected = false; multiplyselected = false; divideselected = false; operationselected = false; operand1 = 0; operand2 = 0; } private void multiply_click(object sender, routedeventargs e) { if (operand1 == 0 && operand2 == 0 && operationselected == false) //operationselected makes sure * cannot pressed more once @ time { //if + clicked , nothing has been stored operand1 yet int32.tryparse(txtdisplay.text, out operand1); //store displayed text operand1 variable } else if (operand1 != 0 && operand2 == 0 && operationselected == false) { //if + clicked , both operand variables stored int32.tryparse(txtdisplay.text, out operand2); //store what's on display operand 2 variable if (addselected) { operand1 = c.addoperands(operand1, operand2); addselected = false; } else if (subtractselected) { operand1 = c.subtractoperands(operand1, operand2); subtractselected = false; } else if (multiplyselected) { operand1 = c.multiplyoperands(operand1, operand2); multiplyselected = false; } else if (divideselected) { operand1 = c.divideoperands(operand1, operand2); divideselected = false; } txtdisplay.text = operand1.tostring(); operand2 = 0; //empty operand2 variable } operationselected = true; multiplyselected = true; } private void divide_click(object sender, routedeventargs e) { if (operand1 == 0 && operand2 == 0 && operationselected == false) { //if + clicked , nothing has been stored operand1 yet int32.tryparse(txtdisplay.text, out operand1); //store displayed text operand1 variable } else if (operand1 != 0 && operand2 == 0 && operationselected == false) { //if + clicked , both operand variables stored int32.tryparse(txtdisplay.text, out operand2); //store what's on display operand 2 variable if (addselected) { operand1 = c.addoperands(operand1, operand2); addselected = false; } else if (subtractselected) { operand1 = c.subtractoperands(operand1, operand2); subtractselected = false; } else if (multiplyselected) { operand1 = c.multiplyoperands(operand1, operand2); multiplyselected = false; } else if (divideselected) { operand1 = c.divideoperands(operand1, operand2); divideselected = false; } txtdisplay.text = operand1.tostring(); operand2 = 0; //empty operand2 variable } operationselected = true; divideselected = true; } private void subtract_click(object sender, routedeventargs e) { if (operand1 == 0 && operand2 == 0 && operationselected == false) { //if + clicked , nothing has been stored operand1 yet int32.tryparse(txtdisplay.text, out operand1); //store displayed text operand1 variable } else if (operand1 != 0 && operand2 == 0 && operationselected == false) { //if + clicked , both operand variables stored int32.tryparse(txtdisplay.text, out operand2); //store what's on display operand 2 variable if (addselected) { operand1 = c.addoperands(operand1, operand2); addselected = false; } else if (subtractselected) { operand1 = c.subtractoperands(operand1, operand2); subtractselected = false; } else if (multiplyselected) { operand1 = c.multiplyoperands(operand1, operand2); multiplyselected = false; } else if (divideselected) { operand1 = c.divideoperands(operand1, operand2); divideselected = false; } txtdisplay.text = operand1.tostring(); operand2 = 0; //empty operand2 variable } operationselected = true; subtractselected = true; } private void add_click(object sender, routedeventargs e) { if (operand1 == 0 && operand2 == 0 && operationselected == false){ //if + clicked , nothing has been stored operand1 yet int32.tryparse(txtdisplay.text, out operand1); //store displayed text operand1 variable } else if (operand1 != 0 && operand2 == 0 && operationselected == false) { //if + clicked , both operand variables stored int32.tryparse(txtdisplay.text, out operand2); //store what's on display operand 2 variable if (addselected) { operand1 = c.addoperands(operand1, operand2); addselected = false; } else if (subtractselected) { operand1 = c.subtractoperands(operand1, operand2); subtractselected = false; } else if (multiplyselected) { operand1 = c.multiplyoperands(operand1, operand2); multiplyselected = false; } else if (divideselected) { operand1 = c.divideoperands(operand1, operand2); divideselected = false; } txtdisplay.text = operand1.tostring(); operand2 = 0; //empty operand2 variable } operationselected = true; addselected = true; } private void equals_click(object sender, routedeventargs e) { int32.tryparse(txtdisplay.text, out operand2); if(addselected){ operationresult = c.addoperands(operand1, operand2); //operand1 = operationresult; } if(subtractselected){ operationresult = c.subtractoperands(operand1, operand2); } if(multiplyselected){ operationresult = c.multiplyoperands(operand1, operand2); } if(divideselected){ operationresult = c.divideoperands(operand1, operand2); } txtdisplay.text = operationresult.tostring(); int32.tryparse(txtdisplay.text, out operand1); addselected = false; subtractselected = false; multiplyselected = false; divideselected = false; operationselected = false; } private void seven_click(object sender, routedeventargs e) { txtdisplay.text = txtdisplay.text == "0" || operationselected ? "7" : txtdisplay.text + "7"; operationselected = false; } private void eight_click(object sender, routedeventargs e) { txtdisplay.text = txtdisplay.text == "0" || operationselected ? "8" : txtdisplay.text + "8"; operationselected = false; } private void nine_click(object sender, routedeventargs e) { txtdisplay.text = txtdisplay.text == "0" || operationselected ? "9" : txtdisplay.text + "9"; operationselected = false; } private void four_click(object sender, routedeventargs e) { txtdisplay.text = txtdisplay.text == "0" || operationselected ? "4" : txtdisplay.text + "4"; operationselected = false; } private void five_click(object sender, routedeventargs e) { txtdisplay.text = txtdisplay.text == "0" || operationselected ? "5" : txtdisplay.text + "5"; operationselected = false; } private void six_click(object sender, routedeventargs e) { txtdisplay.text = txtdisplay.text == "0" || operationselected ? "6" : txtdisplay.text + "6"; operationselected = false; } private void one_click(object sender, routedeventargs e) { txtdisplay.text = txtdisplay.text == "0" || operationselected ? "1" : txtdisplay.text + "1"; operationselected = false; } private void two_click(object sender, routedeventargs e) { txtdisplay.text = txtdisplay.text == "0" || operationselected ? "2" : txtdisplay.text + "2"; operationselected = false; } private void three_click(object sender, routedeventargs e) { txtdisplay.text = txtdisplay.text == "0" || operationselected ? "3" : txtdisplay.text + "3"; operationselected = false; } private void btn_0_click(object sender, routedeventargs e){} private void off_click(object sender, routedeventargs e) { c_click(sender, e); txtdisplay.foreground = new solidcolorbrush(colors.white); } private void on__click(object sender, routedeventargs e){} private void on_click(object sender, routedeventargs e) { c_click(sender, e); txtdisplay.foreground = new solidcolorbrush(colors.black); } } }
take @ msdn article access modifiers. if want protect class external assemblies make internal
if want different have several suggestions. (i'm not sure need posted suggestions now)
method 1: getter-only property
public class myclass { // read-only access outside, no 'set' public object restrictedmember { get; private set; } }
method 2: protected modifier
with modifier derived class can access member.
public class baseclass { protected object restrictedmember; } public derivedclass : baseclass { public derivedclass() : base() { // can access base class's protected member restrictedmember = new object(); } }
method 3: delegates
for methods can declare own delegate.
public delegate string mydelegate(int a, int b); public class myclass { // should make readonly or getter-only property if don't want outside changes public mydelegate restrictedmethod = _restrictedmethod; private string _restrictedmethod(int a, int b) { return (a + b).tostring(); } } public class test { public void it(myclass mc) { // can call method , result string sumasstr = mc.restrictedmethod(3, 5); } }
i'm not tested them. minor bugs/syntax errors can fixed.
Comments
Post a Comment