Reputation: 27
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.
Upvotes: 0
Views: 1751
Reputation: 75668
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:
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));
}
}
Upvotes: 1
Reputation: 338
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
Upvotes: 0