C# - 为什么我不能在另一个类中更改我的int变量?

时间:2014-01-31 18:14:50

标签: c# compiler-errors global-variables

好的,我有以下变量:

    public static int cookieCount = 0;
    public static int cursorCount = 0;
    public static int granCount = 0;
    public static int farmCount = 0;
    public static int mineCount = 0;
    public static int shipCount = 0;
    public static int alcCount = 0;
    public static int portalCount = 0;
    public static int timeCount = 0;

    public static int clickGain = 1;
    public static int cursorGain = 1;
    public static int granGain = 5;
    public static int farmGain = 10;
    public static int mineGain = 50;
    public static int shipGain = 100;
    public static int alcGain = 500;
    public static int portalGain = 10000;
    public static int timeGain = 123456;

    public static int cursorPrice = 20;
    public static int granPrice = 100;
    public static int farmPrice = 500;
    public static int minePrice = 1000;
    public static int shipPrice = 10000;
    public static int alcPrice = 50000;
    public static int portalPrice = 500000;
    public static int timePrice = 1500000;

    public static float cursorSpeed = 1F;
    public static float granSpeed = 2F;
    public static float farmSpeed = 5F;
    public static float mineSpeed = 5F;
    public static float shipSpeed = 10F;
    public static float alcSpeed = 15F;
    public static float portalSpeed = 10F;
    public static float timeSpeed = 15F;

我在Global.cs中有这个。所以,当我在Form1中时,我使用它:

    private void cookie_Click(object sender, EventArgs e)
    {
        Global.cookieCount + Global.clickGain;
    }

我收到错误。

Only assignment, call, increment, decrement, and new object expressions can be used as a statement.

所以常识告诉我静态变量不可更改,但将static更改为dynamic会抛出这些错误:

Invalid token 'int' in class, struct, or interface member declaration

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

'CookieClicker.Global.cookieCount' is inaccessible due to its protection level

An object reference is required for the non-static field, method, or property 所以我很难知道如何做到这一点。

我想要的只是当cookie_Click运行时,Global.clickGain将被添加到cookieCount中,但我希望变量是全局的。

1 个答案:

答案 0 :(得分:5)

您没有将结果分配给任何内容。

将结果分配给Global.cookieCount,如下所示:

Global.cookieCount += Global.clickGain;

或者喜欢这个

Global.cookieCount = Global.cookieCount + Global.clickGain;