c# - Making decisions from a menu -


this first post , first semester in c#. have homework assignment i've been working on days , can't figure out. i'm going try explain best can.

so have create class call 2 other class , compile classes print. user suppose select number menu , number suppose math operation , print answer. can't code produce selection , perform math operation.

here first class.

class mainmodule {     static void main()     {         string assignment = "assignment#3b-math operations modified";          mathoperationui mynumber = new mathoperationui();         mynumber.mathmainmodule();          console.readline(); 

here second class.

class mathoperations {     int firstoperand;     int secondoperand;      public int firstoperand     {                 {             return firstoperand;         }         set         {             firstoperand = value;         }     }      public int secondoperand     {                 {             return secondoperand;         }         set         {             secondoperand = value;         }     }      public mathoperations()     {         firstoperand = 0;         secondoperand = 0;     }      public double add()     {         double theaddition;         theaddition = (firstoperand + secondoperand);         return theaddition;     }      public double subtract()     {         double thesubtraction;         thesubtraction = (firstoperand - secondoperand);         return thesubtraction;     }      public double multiply()     {         double themultiplication;         themultiplication = (firstoperand * secondoperand);         return themultiplication;     }      public double divide()     {         double thedivision;         thedivision = (float)firstoperand / (float)secondoperand;         return thedivision; 

and last class that's giving me problem.

class mathoperationui {     public mathoperationui()     {     }     public void mathmainmodule()     {         int firstoperand;         int secondoperand;          displaymenu();                   mathoperations usersmathoperations;          firstoperand = promptforinterger("first");         secondoperand = promptforinterger("second");          usersmathoperations = new mathoperations ();     }      public void displaymenu()     {         console.writeline("\n\tmenu");         console.writeline("****************************");         console.writeline("1: addition operation");         console.writeline("2: subtraction operation");         console.writeline("3: multiplication operation");         console.writeline("4: division operation");         console.writeline("5: exit");         console.writeline("****************************");     }      static int processmenu(int choice)     {         if (choice == 1)             console.writeline("\nwhen adding number {0} , {1}, answer {2}", mynumber.firstoperand, mynumber.secondoperand, addition);         else             if (choice == 2)                 console.writeline("\nwhen subtracting number {0} , {1}, answer {2}", mynumber.firstoperand, mynumber.secondoperand, subtraction);             else                 if (choice == 3)                     console.writeline("\nwhen multipling number {0} , {1}, answer {2}", mynumber.firstoperand, mynumber.secondoperand, multiplication);                 else                     if (choice == 4)                         console.writeline("\nwhen dividing number {0} , {1}, answer {2:f2}", mynumber.firstoperand, mynumber.secondoperand, division);                     else                         if (choice == 5)                             return 0;     }      static int promptforinterger(string position)     {         console.writeline("\n\nenter {0} number:\t", position);         return (int.parse(console.readline()));     } 

mynumber not exist in current context because should either exists in processmenu function of should exist in global/instance context. in right track missing points. declare mathoperations object intance variable in mathoperationsui class in mathmainmodule set firstoperand , secondoperand of object , call processmenu. in process menu instead of using mynumber use object declared instance variable (mathoperations), , call appropriate functions (add,multiply etc) let me know if working. have working version post if can't it.

this below can accessed in main method because declared. if declared in method can accessed in method unless pass parameter method.

 mathoperationui mynumber = new mathoperationui(); 

besides dont want call mynumber.firstoperand because mynumber mathoperationui type firstoperand in mathoperations not in ..ui.

your mathoperationui should below. mathoperations object declared in class (mathoperationui) outside of method. means can access object method within mathoperationui. should set properties of mathoperations (first , second operand) user input promptforinterger. should call processmenu method process inputs.

public class mathoperationui {     mathoperations usersmathoperations;      public mathoperationui()     {         usersmathoperations = new mathoperations();     }      public void mathmainmodule()     {         displaymenu();         usersmathoperations.firstoperand = promptforinterger("first");         usersmathoperations.secondoperand = promptforinterger("second");         console.writeline("\n\nenter coice");         processmenu(int.parse(console.readline()));     } 

now can access object process method. , can first , second operand , call add, multiply etc. methods.

console.writeline("\nwhen adding number {0} , {1}, answer {2}", usersmathoperations.firstoperand, usersmathoperations.secondoperand, usersmathoperations.add()); 

finally here piece working https://dotnetfiddle.net/5le63l

hope makes things bit clearer.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -