-3

The first snipet when i try to assign an instance member variable in a class of which volume = length * width * height. i get an error saying a field initializer cant reference the non-static field, method or property "Box.height".

namespace Myproject
{
    class Box
    {
        public int length;
        public int height;
        public int width;
        public int volume = length * width * height;
        public void DisplayBox()
        {
            Console.WriteLine("The height of box is length {0} * height {1} * width {2} and volume is {3}", length, height, width, volume =);
        }
    }
}

the second code snipet when i make the all variable to a static it allow volume to take the given assignment not sure how to explain myself but can someone explain to me.

namespace Myproject
{
    class Box
    {
        public  static int length;
        public static int height;
        public static int width;
        public static int volume = length * width * height;
        public void DisplayBox()
        {
            Console.WriteLine("The height of box is length {0} * height {1} * width {2} and volume is {3}", length, height, width, volume);
        }
    }
}

Third snipet Why is it only in the method that am allow to assign volume to the variable for calculation?

namespace Myproject
{
    class Box
    {
        public int length;
        public int height;
        public int width;
        public int volume;
        public void DisplayBox()
        {
            Console.WriteLine("The height of box is length {0} * height {1} * width {2} and volume is {3}", length, height, width, volume = length * width * height);
        }
    }
}
ganiyu
  • 65
  • 1
  • 7
  • 3
    Please don't post pictures like this. Do anything else. Describe it. Include the code. Whatever you want someone to read, please include it as text. – Scott Hannen Jul 04 '19 at 19:37
  • Did you try using properties? Post the code as text here rather than images. – But I'm Not A Wrapper Class Jul 04 '19 at 19:37
  • 1
    `public int Volume { get => length * width * height; } ` And follow @ScottHannen advice – Kevin Kouketsu Jul 04 '19 at 19:38
  • 1
    The assignment to `volume` is “initialization”: It happens once when the instance is created. But height and width etc don’t have values yet at that time, so the compiler won’t let you do that. And anyway, it would only happen once — it’s just a one-time assignment. Kevin’s suggestion turns `volume` into a “property”: it’s not just a value; it has code that calculates a value every time you look at it. I think that’s what you really want. – 15ee8f99-57ff-4f92-890c-b56153 Jul 04 '19 at 19:49
  • Hello but why does it allow initialization when variable becomes static also volume accept the given assignment but when it a instance variable the only way i can assign volume is through the method, or a constructor or a property why is this, why cant i assign volume when as a instance member variable. – ganiyu Jul 04 '19 at 19:54

1 Answers1

0
class Box
    {
        public int Length { get; set; }
        public int Height { get; set; }
        public int Width { get; set; }
        public int Volume => Length * Width * Height;
        public void DisplayBox()
        {
            Console.WriteLine("The height of box is length {0} * height {1} * width {2} and volume is {3}", Length, Height, Width, Volume);
        }
        public Box(){}
        public Box(int length, int height, int width)
        {
            Length = length;
            Height = height;
            Width = width;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Using custom constructor
            new Box(5,10,4).DisplayBox();
            //Using default constructor
            new Box {Length = 5, Height = 10, Width = 4}.DisplayBox();
            //Using class instance
            var box = new Box();
            box.Length = 5;
            box.Height = 10;
            box.Width = 4;
            box.DisplayBox();
            Console.ReadKey();
        }
    }
Taurus999able
  • 61
  • 1
  • 1