Domanda

Apologies if this has been asked before but I'm new to programming.

Question: Whenever I create properties, I get a message stating I should convert it to an auto-property.

My program runs so should I be worried about this?

using System;

namespace ConsoleApplication12
{
struct Calculator
{

    // Properties for returning addition numbers 
    private static long _add1, _add2;
    private static long _addtotal;

    public static long AddNumberOne
    {
        get { return _add1; } set { _add1 = value; } 

    }

    public static long AddNumberTwo
    {
        get { return _add2; } set { _add2 = value; } 

    }

    // Properties for returning multiplication numbers 
    private static long _mul1, _mul2;
    private static long _multiplytotal;

    public static long MulNumberOne
    {
        get { return _mul1; } set { _mul1 = value; } 

    }

    public static long MulNumberTwo
    {
        get { return _mul2; } set { _mul2 = value; } 

    }

    // Properties for returning division numbers 
    private static double _div1, _div2;
    private static double _dividetotal;

    public static double DivNumberOne
    {
        get { return _div1; } set { _div1 = value; } 

    }

    public static double DivNumberTwo
    {
        get { return _div2; } set { _div2 = value; } 

    }

    // Properties for returning minus numbers 
    private static double _min1, _min2;
    private static double _minustotal;

    public static double MinNumberOne
    {
        get { return _min1; } set { _min1 = value; } 

    }

    public static double MinNumberTwo
    {
        get { return _min2; } set { _min2 = value; } 

    }

    // Property for returning program run state 
    private static bool _run = true;

    public static bool RunProgram
    {
        get { return _run; } set { _run = value; }
    }

    // Properties for returning total after calculation is made 
    public static long TotalAddition
    {
        get { return _addtotal; } set { _addtotal = value; } 

    }

    public static long TotalMultiply
    {
        get { return _multiplytotal; } set { _multiplytotal = value; } 

    }

    public static double TotalDivide
    {
        get { return _dividetotal; } set { _dividetotal = value; } 

    }

    public static double TotalMinus
    {
        get { return _minustotal; } set { _minustotal = value; } 

    }

    // Methods for calculating total 
    public static long AddCalculation(long a, long b)
    {
        TotalAddition = a + b;
        return TotalAddition;
    }

    public static long MultiplyCalculation(long a, long b)
    {
        TotalMultiply = a*b;
        return TotalMultiply;
    }

    public static double DivideCalculation(double a, double b)
    {
        TotalDivide = a/b;
        return TotalDivide;
    }

    public static double MinusCalculation(double a, double b)
    {
        TotalMinus = a - b;
        return TotalMinus;
    }

    // Properties for user input
    private static string _input;
    private static long _inputConv;

    public static string InputOption
    {
        get { return _input; }
        set { _input = value; }
    }

    public static long InputConversion
    {
        get { return _inputConv; }
        set { _inputConv = value; }
    }

    public static void DisplayInstructionMenuAndSelectCalculatioType()
    {
        Console.WriteLine("What operation would you like to carry out?\n");
        Console.WriteLine("1. Addition\n" +
                          "2. Multiplication\n" +
                          "3. Subtraction\n" +
                          "4. Division");

        InputOption = Console.ReadLine();
        InputConversion = Convert.ToInt64(InputOption);
    }

    public static void UserInputForAdditionCalculation()
    {
        Console.WriteLine("*******ADDITION*******");
        Console.WriteLine("Enter number 1: ");
        InputOption = Console.ReadLine();
        AddNumberOne = Convert.ToInt64(InputOption);
        Console.WriteLine("Enter number 2: ");
        InputOption = Console.ReadLine();
        AddNumberTwo = Convert.ToInt64(InputOption);
        TotalAddition = AddCalculation(AddNumberOne, AddNumberTwo);
        Console.WriteLine("\n\nResult: {0} + {1} = {2}", AddNumberOne, AddNumberTwo, TotalAddition);
        Console.Write("Another calculation? YES/NO: ");
        InputOption = Console.ReadLine();
    }

    public static void UserInputForMultiplyCalculation()
    {
        Console.WriteLine("*******MULTIPLY*******");
        Console.WriteLine("Enter number 1: ");
        InputOption = Console.ReadLine();
        MulNumberOne = Convert.ToInt64(InputOption);
        Console.WriteLine("Enter number 2: ");
        InputOption = Console.ReadLine();
        MulNumberTwo = Convert.ToInt64(InputOption);
        TotalMultiply = MultiplyCalculation(MulNumberOne, MulNumberTwo);
        Console.WriteLine("\n\nResult: {0} x {1} = {2}", MulNumberOne, MulNumberTwo, TotalMultiply);
        Console.Write("Another calculation? YES/NO: ");
        InputOption = Console.ReadLine();
    }

    public static void UserInputForSubtractCalculation()
    {
        Console.WriteLine("*******SUBTRACT*******");
        Console.WriteLine("Enter number 1: ");
        InputOption = Console.ReadLine();
        MinNumberOne = Convert.ToInt64(InputOption);
        Console.WriteLine("Enter number 2: ");
        InputOption = Console.ReadLine();
        MinNumberTwo = Convert.ToInt64(InputOption);
        TotalMinus = MinusCalculation(MinNumberOne, MinNumberTwo);
        Console.WriteLine("\n\nResult: {0} - {1} = {2}", MinNumberOne, MinNumberTwo, TotalMinus);
        Console.Write("Another calculation? YES/NO: ");
        InputOption = Console.ReadLine();
    }

    public static void UserInputForDivisionCalculation()
    {
        Console.WriteLine("*******DIVISION*******");
        Console.Write("Enter number 1: ");
        InputOption = Console.ReadLine();
        DivNumberOne = Convert.ToInt64(InputOption);
        Console.Write("Enter number 2: ");
        InputOption = Console.ReadLine();
        DivNumberTwo = Convert.ToInt64(InputOption);
        TotalDivide = DivideCalculation(DivNumberOne, DivNumberTwo);
        Console.WriteLine("\n\nResult: {0} / {1} = {2}", DivNumberOne, DivNumberTwo, TotalDivide);
        Console.Write("Another calculation? YES/NO: ");
        InputOption = Console.ReadLine();
    }

    public static void DisplayMenuOrExitProgram()
    {
        switch (InputOption)
        {
            case "YES":
                DisplayInstructionMenuAndSelectCalculatioType();
                break;
            case "NO":
                RunProgram = false;
                break;
            default:
                RunProgram = false;
                break;
        }
    }

    public static void SwitchCalculationsAndDisplayResults()
    {
        while (RunProgram)
        {
            switch (InputConversion)
            {
                case 1:
                    UserInputForAdditionCalculation();
                    DisplayMenuOrExitProgram();
                    break;
                case 2:
                    UserInputForMultiplyCalculation();
                    DisplayMenuOrExitProgram();
                    break;
                case 3:
                    UserInputForSubtractCalculation();
                    DisplayMenuOrExitProgram();
                    break;
                case 4:
                    UserInputForDivisionCalculation();
                    DisplayMenuOrExitProgram();
                    break;
                default:
                    Console.WriteLine("Something went wrong, sorry");
                    break;
            }
        }
    }
}
}
È stato utile?

Soluzione

You can create the shorter proeprty called Auto-Properties:

public static long MulNumberOne { get; set; }

You can save a few lines of code, but the compiler will then generate the same IL at compile time.

The properties of your class/struct can be shortened to the following:

struct Calculator
{
    // Properties for returning addition numbers 
    public static long AddNumberOne { get; set; }
    public static long AddNumberTwo { get; set; }

    // Properties for returning multiplication numbers 
    public static long MulNumberOne { get; set; }
    public static long MulNumberTwo { get; set; }

    // Properties for returning division numbers
    public static double DivNumberTwo { get; set; }

    // Properties for returning minus numbers 
    public static double MinNumberOne { get; set; }
    public static double MinNumberTwo { get; set; }

    // Property for returning program run state 
    public static bool RunProgram { get; set; }

    // Properties for returning total after calculation is made 
    public static long TotalAddition { get; set; }
    public static long TotalMultiply { get; set; }
    public static double TotalDivide { get; set; }
    public static double TotalMinus { get; set; }

    // Properties for user input
    public static string InputOption { get; set; }
    public static long InputConversion { get; set; }

    // METHODS
}

This will generate the same output after compilation, but is much better to read IMO.
Instead of _add1 you need to use also AddNumberOne inside the class, which also calls the getter method and is marginally slower (only matters at >100k calls per second).

Altri suggerimenti

It mean that You don't use back fields and You don't need it, Your propery can be

public static double MinNumberOne
{
    get; set;

}

Compile will create back field for You.

You shouldn't wory about that, auto-prop is a syntax sugar.

You shouldn't be worried about it. Auto Properties are the same properties you are using now only in a shorter way. They are used when the get and set accessors arent changed. Here a example from your code:

public static double MinNumberOne
{
     get { return _min1; } set { _min1 = value; } 
}

And this how it could be. This is an Auto Property.

public static double MinNumberOne { get; set; }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top