0

First at all, i started to learn C# a few days ago, at the moment i'm in the "Set/Get" Chapter. So every new thing that comes in my tutorial i try to apply it on my small application. Even tho if i do not understand the concepts i go the alternative places and look for suggestions, which is not this case.

I tought that maybe in the Class Calculator i could add a set and get so i do not have to declare the num1, num2 variables on each method? (Sumar, Restar, Division, Multiplicacion)

class Calculo
{

    public static void Suma()
    {

        int num1, num2 = 0;
        Console.Write(" Ingrese el primer numero que quiere sumar: ");
        num1 = Convert.ToInt32(Console.ReadLine());
        Console.Write(" Ingrese el segundo numero que quiere sumar: ");
        num2 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(" Resultado es:   " + (num1 + num2));
    }

    public static void Resta()
    {

        int num1, num2 = 0;
        Console.Write(" Ingrese el primer numero que quiere restar: ");
        num1 = Convert.ToInt32(Console.ReadLine());
        Console.Write(" Ingrese el segundo numero que quiere restar: ");
        num2 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(" Resultado es: " + (num1 - num2));
    }

    public static void Division()
    {

        int num1, num2 = 0;
        Console.Write(" Ingrese el primer numero que quiere Dividir: ");
        num1 = Convert.ToInt32(Console.ReadLine());
        Console.Write(" Ingrese el segundo numero que quiere Dividir: ");
        num2 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(" Resultado es: " + (num1 / num2));
    }

    public static void Multiplicacion()
     {

        int num1, num2 = 0;
        Console.Write(" Ingrese el primer numero que quiere multiplicar: ");
        num1 = Convert.ToInt32(Console.ReadLine());
        Console.Write(" Ingrese el segundo numero que quiere multiplicar: ");
        num2 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(" Resultado es: " + (num1 * num2));
    }

}

Thank you!

@Joshua George

So is this the correct way to use this new class to my program?

...

    public static void Multiplicacion()
     {

        MyClass numbers = new MyClass();
        Console.Write(" Ingrese el primer numero que quiere multiplicar: ");
        numbers.Num1 = Convert.ToInt32(Console.ReadLine());
        Console.Write(" Ingrese el segundo numero que quiere multiplicar: ");
        numbers.Num2 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(" Resultado es: " + (numbers.Num1 * numbers.Num2));
    }

...

  • Since each method is completely responsible for asking for the input, making the calculation, and then outputting the result, you don't need shared state. Properties are used for shared state, in this case you don't need it. You can get rid of 1 line of code from each method by moving the declaration of the variable down to where you initialize it from ToInt32, also note that `int num1, num2 = 0;` will only initialize `num2`, `num1` is still left as uninitialized. – Lasse V. Karlsen Apr 15 '20 at 09:33
  • You should look up what [static](https://stackoverflow.com/questions/9924223/static-vs-non-static-class-members) means. Then you could try to create a method with parameters and return a result – Jeroen van Langen Apr 15 '20 at 09:33
  • @LasseV.Karlsen so in my particular case, lets say i want to add another option to calculate the area and circumference of a circle, PI could be used as a properties. is that correct? –  Apr 15 '20 at 09:43
  • PI already exists, as `Math.PI`, but if not, then yes, you could do it that way. – Lasse V. Karlsen Apr 15 '20 at 09:44
  • @LasseV.Karlsen didn't get into Math Class...Thx!!! –  Apr 15 '20 at 09:47

1 Answers1

-1

So the reason for why we use get and set is for encapsulation, meaning that we want to make sure sensitive data is hidden. In your case you could as an example create a new class (MyClass) and have the properties num1 and num2 inside that class. Inside MyClass you could do the following:

class MyClass
{

    private int _num1 = 0;
    private int _num2 = 0;

    public int Num1
    {
        get { return _num1;}
        set { _num1 = value;}
    }

    public int Num2
    {
        get { return _num2;}
        set { _num2 = value;}
    } 
}

And then inside your Calculo class you could do:

MyClass numbers = new MyClass()
numbers.Num1 = Convert.ToInt32(Console.Readline())
numbers.Num2 = Convert.ToInt32(Console.Readline())

Edit: An alternative way of how you could implement MyClass would be

class MyClass {
   public int num1 {get;set;}
   public int num2 {get;set;}
}

this way you use auto-implemented properties instead of having to write the get-set logic explicitly.