Static instance variables in classes with C#

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Static instance variables in classes with C#



I am trying to teach myself C#. From the book I am using, I created a Color class and a Ball class. Here is the pertinent code:


public class Color

public int Red;
public int Blue;
public int Green;
public int Alpha;

public Color(int red, int blue, int green, int alpha)

this.Red = red;
this.Blue = blue;
this.Green = green;
this.Alpha = alpha;




This creates the Color class and allows me to assign values to the standard RGB color spectrum we are used to using the Color constructor.



Then I have a method called GetRed(), also within the Color class, which returns the value I set for Red when I create the color in the constructor.


public int GetRed()

return Red;



Finally, I have another class called Ball, where I declare an instance variable color1 to get the Red value from the Color class.


class Ball

public int color1 = Color.GetRed();



The error I get with the color1 declaration in the Ball class is An object reference is required for the non-static field, method, or property "Color.GetRed()".
The fix that Visual Studio suggested was to reference the namespace and the class in the variable declaration, so in my case,public int color1 = Hello_World.Color.GetRed()
and to make the color variables I created in my Color class and the GetRed() method static. This fixes my problem, but I'm wondering if there is another way to fix this, where I don't have to make the variables or the method static. I'm sorry if I didn't explain this very well. I'm just starting out in C#, so if you need clarification, please tell me in a response. Thanks.


An object reference is required for the non-static field, method, or property "Color.GetRed()"


public int color1 = Hello_World.Color.GetRed()


Color


GetRed()





You should post a minimal variant of the code you made.
– Jeroen Heier
Aug 12 at 17:43





Instead of Color.GetRed() you should create an instance and then call the method of this instance
– royalTS
Aug 12 at 17:54


Color.GetRed()





How to create a Minimal, Complete, and Verifiable example.
– Erik Philips
Aug 12 at 18:09






You need to instanciate your object: Color myColor = new Color(); After this you can call its methods: myColor.GetRed();
– Aldert
Aug 12 at 18:19





2 Answers
2



public int color1 = Color.GetRed(); <- accessing static method from class Color.



public int color1 = new Color().GetRed(); <- accessing instance method from instance of class Color.



or


public class Ball

private Color _color;

public Ball(Color color)

_color = color;


public int GetColorRedValue()

return _color.GetRed();




and


var color = new Color();
var ball = new Ball(color);

Console.WriteLine(ball.GetColorRedValue()); <- print red color value



You should really create a MCVE. Without one we are left guessing. So I guess this is what your books wants you to do:


class Color

// ...


class Ball

Color SurfaceColor = new Color(/*...*/);



Well, heck, why not, let's go the extra mile. Here is how I would implement the Color - Ball models:


Color


Ball


public static class Colors

public static readonly Color Red = new Color(255, 0, 0);
public static readonly Color Green = new Color(0, 255, 0);
public static readonly Color Blue = new Color(0, 0, 255);

public static readonly Color White = new Color(255, 255, 255);
public static readonly Color Black = new Color(0, 0, 0);


public class Color

public int R;
public int G;
public int B;

public Color() : this(Colors.Black)

public Color(Color other) : this(other.R, other.G, other.B)

public Color(int red, int green, int blue)

R = red;
G = blue;
B = blue;



public class Ball

public Color SurfaceColor get; private set;

public Ball(Color surfaceColor)

SurfaceColor = surfaceColor;



public class Program

public static void Main()

Ball blueBall /* huh */ = new Ball(Colors.Blue);
Ball redBall = new Ball(Colors.Red);
Ball maroonBall = new Ball(new Color(128, 0, 0));







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard