Tinca Ketano-Leonard Tinca Ketano-Leonard - 16 days ago 4
C# Question

How can i resolve this error?C#

i have a private method is ball.cs class in witch i want to modify a bool type from margins.cs class, but i get the Error An object reference is required for the non-static field, method, or property 'PingPong.Margins.win'.
Can someone help me with this?

this is the ball class code :

namespace PingPong
{
public class Ball
{

private PictureBox ball;
Random rand= new Random();
Player leftSidePlayer, rightSidePlayer;

int xSpeed, ySpeed;

public Ball(PictureBox aBall, Player leftSidePlayer, Player rightSidePlayer)
{

this.ball = aBall;
this.leftSidePlayer = leftSidePlayer;
this.rightSidePlayer = rightSidePlayer;
xSpeed = 1;
ySpeed = 2;
resetBall();
}

internal void processmove()
{
var bottom = Margins.bottomOfWorld - ball.Height;
DoMove();

if(ball.Location.Y >= bottom || ball.Location.Y <= Margins.topOfWorld)
{
ySpeed *= -1;
}

if (ball.Location.X <= Margins.leftOfWorld)
{
Score(leftSidePlayer);
}
else if (ball.Location.X >= Margins.rightOfWorld - ball.Width)
{
Score(rightSidePlayer);
}

if ((leftSidePlayer.paddle.Bounds.IntersectsWith(ball.Bounds)) || (rightSidePlayer.paddle.Bounds.IntersectsWith(ball.Bounds)))
{
xSpeed *= -1;
if ((ySpeed <= 6 && ySpeed >=-6) && (xSpeed <= 5 && xSpeed >=-5) )
{
if(ySpeed < 0)
{
ySpeed -= 1;
}else
{
ySpeed += 1;
}

if (xSpeed < 0)
{
xSpeed -= 1;

}
else
{
xSpeed += 1;
}
}
}


}

private int DoMove()
{
var bottom = Margins.bottomOfWorld - ball.Height;
ball.Location = new Point(ball.Location.X + xSpeed, Math.Max(Margins.topOfWorld, Math.Min(bottom, ball.Location.Y + ySpeed)));
return bottom;
}

private void Score(Player winningPlayer)
{
winningPlayer.scoreNumber++;

if(winningPlayer.scoreNumber == 7)
{
if(winningPlayer == leftSidePlayer )
{
Margins.win = true;
}else if(winningPlayer == rightSidePlayer)
{
Margins.win = false;
}


}


resetBall();
}

private void resetBall()
{
ball.Location = new Point((Margins.leftOfWorld + Margins.rightOfWorld) / 2, (Margins.bottomOfWorld + Margins.topOfWorld) / 2);
do
{
xSpeed = rand.Next(-3, 3);
ySpeed = rand.Next(-3, 3);
} while(Math.Abs(xSpeed) + Math.Abs(ySpeed) <= 3);

}

}


}

and this is the margins class code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PingPong
{
public class Margins
{ //Marimea ferestrei 1000; 600
public const int topOfWorld = 0, bottomOfWorld = 560, leftOfWorld =0, rightOfWorld = 1000;
public bool? win = null;
}
}


the error i get is in private void score when i use the margins.win = true
and margins.win = false .

Answer

You have a class with some static (or constant) variables now. Next to that, there is the instance variable win. That means you have to create an instance of Margins in order to get or set it.

If you really need a static class since there can always be just one Margins instance, make the class and its variables static:

public static class Margins
{   //Marimea ferestrei 1000; 600
    public const int topOfWorld = 0, bottomOfWorld = 560, leftOfWorld =0, rightOfWorld = 1000;
    public static bool? win = null;
}

If not, make a proper instance of Margins:

private Margins margins = new Margins();

this.margins.win = true;