-1

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 an solution on github, but I haven't make it work yet. I don't know where I made mistake.

My Code

using System;

namespace ClassDemo2
{
    class Box
    {
        private double _width;
        private double _height;
        private double _length;

        public double Width
        {
            get { return _width; }
            set { this._width = Width; }
        }

        public double Height
        {
            get { return _height; }
            set { this._height = Height; }
        }

        public double Length
        {
            get { return _length; }
            set {this._length = Length; }
        }

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

    }

    public class Program
    {
        static void Main(string[] args)
        {
            Box box = new Box();

            //Set value
            box.Width = 12;
            box.Height = 12;
            box.Length = 12;

            //Get value
            double width = box.Width;
            double height = box.Height;
            double length = box.Length;

            Console.WriteLine("Box properties");
            Console.WriteLine("Width: {0}", width);
            Console.WriteLine("Height: {0}", height);
            Console.WriteLine("Length: {0}", length);
            Console.WriteLine("Volume: {0}", box.volume());
        }
    }

}

Console Window

enter image description here

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
kawa
  • 207
  • 1
  • 7
  • 3
    why do you have empty setters like `set { ; }` ? they basically do nothing, effectively making your objects readonly – Iłya Bursov Jun 10 '22 at 21:40
  • 5
    `set { this._width = value; }` – LarsTech Jun 10 '22 at 21:45
  • 4
    In C#, a setter must always use **`value`** as in: `set { this._width = value; }`. Same for the other properties. – Peter B Jun 10 '22 at 21:45
  • @Iłya Bursov thanks for pointing that out, I was copy pasting code and somehow I thought `set{ ; }` automatically set the value. but even I added some code in there, the resul on console window is the same – kawa Jun 10 '22 at 21:47
  • @kawa that would be `set;` without the {}. But then your getter should also be automatic and there is no private field, as shown in RCS' answer. – CompuChip Jun 10 '22 at 21:52
  • That's not what tags are for; they're for categorizing questions. Java people who know C# will be looking at the C# tag. – Ryan M Jun 14 '22 at 07:40

2 Answers2

3

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;
    }
}
RCS
  • 104
  • 5
  • 2
    Perhaps you can expand the code example to show the whole class, to emphasize that when using auto properties you don't need the `private double ...` anymore – CompuChip Jun 10 '22 at 21:53
  • This is very simple! thanks! `public string FirstName { get; set; } = "Jane";` it's cool that you can also declare a default value like this. Thank you very much! – kawa Jun 10 '22 at 21:54
0
    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://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties)