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);
}
}
}