tags:

views:

70

answers:

4

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.

+1  A: 

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;
  }
}
BlueMonkMN
Sorry, I'm using `this.screenWidth` and `this.screenHeight` in my main game class, and in my paddle class I am trying to access them simply by using `screenWidth` and `screenHeight`. I also tried using `Breakout.screenWidth` and `Breakout.screenHeight` but I get the same error.
Wen
@Wen, `this` won't work because `this` refers to `Paddle` and not `Breakout` since this code is inside `Paddle`. You'll need to pass it a reference to `Breakout` as I explain in my answer.
Kirk Woll
No, I am only using `this` when I am setting the values in my main game class where these variables are declared. When I'm trying to access them from another class, I am not using `this`.
Wen
You can't simply use "screenWidth"... you need to refer to some instance of the main game class. Or you need to make the two member of the main game class "static" and then you can just use the main game class name before the ".screenWidth" to access it. Static means that you don't need to track the members independently by instance (there will only be one version of the member for all instances).
BlueMonkMN
I added example code to clarify.
BlueMonkMN
Ahh, setting them as `static` works. Thanks :)
Wen
+1  A: 

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.

Kirk Woll
A: 

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;
Tyler
@ttreat31, I suspect the OP doesn't want to create a new instance of his game every time he needs the `screenHeight`...
Kirk Woll
Ah, that makes sense. But my paddle class is an instance of my breakout class, so isn't it wrong to send an instance back?
Wen
@Wen, I'm a little unclear on what you mean when you say your Paddle class is an instance of Breakout.
Tyler
In my `Breakout` class, I do this: `paddle = new Paddle();`. So the breakout class is making an instance of `Paddle`. I want these variables to be accessibly by every class I intend to add without passing them.
Wen
@Wen, then perhaps these variables should be static?
Tyler
+2  A: 

make it a public static

soooo...

public static int screenWidth;

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

Spooks