简体   繁体   中英

Public variable not accessible in my classes

I am using the XNA framework to make one of my first games. In it's main game class ( Breakout.cs ), I put this:

public int screenHeight;
public int screenWidth;

And in the Initialize method:

this.screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
this.screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;

But when I try and access it from my Paddle class I get this error:

The name 'screenWidth' does not exist in the current context

I always thought that setting a variable to public would make it accessible any where?

Help is appreciated, thanks.

I'm not familiar with XNA (though I've been meaning to look into it), but it sounds like you're trying to refer to members of one class from within another class. In order to do that, you have to actually refer to the first class. You said screenHeight and screenWidth are declared in the main game class, but then you said you were trying to access them in the Paddle class. But if you use "this" you can only access members of Paddle, not members of the "main game" class, whatever that's called. You need to refer to some reference to the main game class (instead of "this") if you want to see the members of it.

Examples:

WRONG:

class MyGame
{
  public int screenWidth;
  public int screenHeight;
  void MainLoop()
  {
     Paddle p = new Paddle();
  }
}

class Paddle
{
  void Initialize()
  {
     int w = screenWidth;
     int h = screenHeight;
  }
}

BETTER:

class MyGame
{
  public int screenWidth;
  public int screenHeight;
  void MainLoop()
  {
     Paddle p = new Paddle(this);
  }
}

class Paddle
{
  MyGame game;

  public Paddle(MyGame game)
  {
     this.game = game;
  }

  void Initialize()
  {
     int w = game.screenWidth;
     int h = game.screenHeight;
  }
}

I always thought that setting a variable to public would make it accessible anywhere?

public makes the field accessible from other classes, but you still need a reference to the instance. Somewhere in class Paddle there should exist a reference back to Breakout since you need it. For example, by having a field of type Breakout called "breakout" that you assign when you instantiate a Paddle . Then, from Paddle , you may call breakout.screenHeight .

Another option is to make Breakout a singleton (are you ever going to have more?). Declare a static field and store your instance of Breakout in there when it's constructed. Then expose an Instance property:

private static Breakout instance = new Breakout();

public static Breakout Instance 
{
    get { return instance; }
}

Then from Paddle :

Breakout.Instance.screenHeight

Not certain if you want instance to instantiate Breakout in the intiailizer since I'm not sure how your game class is currently being instantiated.

You need to create an instance of the class Breakout.

Breakout breakout = new Breakout();

Then you can access that object's public fields.

int height = breakout.screenHeight;

make it a public static

soooo...

public static int screenWidth;

and then just write paddle.maxY = Breakout.screenWidth;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM