简体   繁体   English

如何更改其他类中的变量?

[英]How do I change a variable from a different class?

I'm trying to change the variable GameWeek from a method in a different class, but I can't get it to work. 我正在尝试从其他类中的方法更改变量GameWeek,但无法使其正常工作。

In my Game1 constructor I create the class 在我的Game1构造函数中,创建类

public GameData currentGameData = new GameData();

The class I'm trying to change the GameWeek variable is created in the Game1 Load method: 我尝试更改GameWeek变量的类是在Game1 Load方法中创建的:

startScreen = new StartScreen(this, globalVariables, currentGameData);

In the StartScreen class I have the following (constructor + next one or two lines): 在StartScreen类中,我具有以下内容(构造函数+接下来的一两行):

GameData thisGameData

public StartScreen(Game1 game, GlobalVariables globalVariables, GameData gameData)
      : base(game)
{
  this.thisGameData = gameData;

I then modify thisGameData in a method in the startScreen class: 然后,我在startScreen类的方法中修改thisGameData:

thisGameData.GameWeek = 1;  //this doesn't work

However, currentGameData in the Game1 class hasn't changed. 但是,Game1类中的currentGameData尚未更改。 This hardly seems surprising, my question is how to I go about actually doing this? 这似乎不足为奇,我的问题是我该如何去做呢? As I understand it, when you pass a class in a constructor only the reference is passed, so even though the reference name is difference shouldn't it point to the same place (as they were set to be the same). 据我了解,当您在构造函数中传递类时,仅传递了引用,因此,即使引用名称不同,也不应将其指向同一位置(因为它们被设置为相同)。

I feel like I've completely lost the plot / got something a bit backward here, but I can't figure this one out. 我觉得我已经完全失去了情节/这里有些落后,但我无法弄清楚。

Any help much appreciated! 任何帮助,不胜感激!

GameData class for reference: GameData类供参考:

public int gameWeek;
public int GameWeek
{
  get { return gameWeek; }
  set
  {
    if (gameWeek > 52)
    {
      gameWeek = 0;
      gameYear += 1;
    }
    else
    {
      gameWeek = value;
    }
  }
}

public int gameYear;

EDIT 编辑

To reclarify the question as I don't feel like I've phrased it very well. 为了澄清这个问题,我觉得我的措辞很好。

If in my Game1 class I write: 如果在我的Game1类中,我这样写:

currentGameData.gameWeek = 1;

Then it works. 然后就可以了。

What I want to achieve is exactly the same as that, but do it from the within the StartScreen class. 我要实现的功能与之完全相同,但是可以从StartScreen类中进行。

If I just write the above in my StartScreen class then it obviously doesn't work because currentGameData doesn't exist. 如果我只是在StartScreen类中编写以上代码,则由于currentGameData不存在,因此显然不起作用。

Sorry, prior post was entered too quickly. 抱歉,之前的帖子输入速度太快。

public int GameWeek
{
  get { return gameWeek; }
  set
  {
    if (value > 52) // value here was gameWeek.
    {
      gameWeek = 0;
      gameYear += 1;
    }
    else
    {
      gameWeek = value;
    }
  }
}

Do you also need to do something in your calling code to determine if the week is greater than 52? 您是否还需要在调用代码中执行某些操作来确定星期是否大于52? Eg, your code will work in the GameWeek property if value is 53, but if it hits 54, the week would still be 0 and the year would increase by one, again. 例如,如果值是53,则您的代码将在GameWeek属性中工作,但是如果值达到54,则星期仍为0,年份又增加一。

EDIT: responding to poster's intended issue of not being able to set a value and have it reflect 编辑:响应海报的预期问题,即无法设置值并使它反映出来

I haven't worked a whole lot with C#, but it seems like you need a reference parameter to do what you are asking for, though it seems like a bad idea to do it that way. 我还没有使用C#进行很多工作,但是似乎您需要一个引用参数来完成您要的内容,尽管这样做似乎是一个坏主意。

startScreen = new StartScreen(this, globalVariables, ref currentGameData);


public StartScreen(Game1 game, GlobalVariables globalVariables, ref GameData gameData)
      : base(game)
{
  this.thisGameData = gameData;
  this.thisGameData.GameWeek = 1;
  // ...
}

BTW, you should put better snippets of code in for us, such as giving us the entire wrapper, like so: 顺便说一句,您应该为我们添加更好的代码片段,例如为我们提供整个包装,如下所示:

// GameData class for reference:
class GameData {
  public int gameWeek;
  public int GameWeek
  {
    get { return gameWeek; }
    set
    {
      if (gameWeek > 52)
      {
        gameWeek = 0;
        gameYear += 1;
      }
      else
      {
        gameWeek = value;
      }
    }
  }

  public int gameYear;
} // end class GameData

That takes a lot less explaining =) 这需要很多的解释=)

将其声明为public static int gameweek;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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