-2

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 .

  • Most c# errors you come across you can just search for on google and you'll find a stackoverflow question that should help. if it doesn't you should include your research in your own question – Sayse Nov 23 '16 at 07:55
  • You trying to edit it like it static variable but it not, if you need it to be static make it static public static bool? win = null; – genichm Nov 23 '16 at 07:57
  • 1
    In addition to this being a duplicate, please read https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ for advice on writing a good question. In particular, try to come up with a [mcve] - most of the code in this question isn't relevant. – Jon Skeet Nov 23 '16 at 07:57
  • https://www.google.com.vn/search?q=An+object+reference+is+required+for+the+non-static+field&oq=An+object+reference+is+required+for+the+non-static+field&aqs=chrome..69i57.629j0j7&sourceid=chrome&ie=UTF-8 – Tân Nov 23 '16 at 07:58

1 Answers1

0

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;
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325