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

 chingueen_306 发布于 2023-01-30 09:36

好的,所以我有以下变量:

    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 个回答
  • 您没有将结果分配给任何内容.

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

    Global.cookieCount += Global.clickGain;
    

    或者像这样

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

    2023-01-30 09:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有