Home > Blockchain >  I cannot set and call value using setter and getter
I cannot set and call value using setter and getter

Time:06-11

Learning Setter and Getter

I am making a console log app where I create a Box class and make an object and set values: width, height, and length using setter and getter. I was referencing enter image description here

CodePudding user response:

The setters in the Box class aren't doing anything. The setters aren't assigning a value to the private fields in the Box class.

You may as well remove the fields altogether and use auto-implemented properties.

For example:

class Box
{
    public double Width { get; set; }

    public double Height { get; set; }

    public double Length { get; set; }

    public double Volume()
    {
        return Width * Height * Length;
    }
}

CodePudding user response:

    class Box
    {
        public double Width { get; set; }
        public double Height { get; set; }
        public double Length { get; set; }

        public double volume()
        {
            return Width * Height * Length;
        }
    }

Use Auto-Implemented Properties (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties)

  •  Tags:  
  • c#
  • Related