1

我对 c# 非常陌生,我正在尝试构建一个小应用程序来帮助一些朋友玩游戏。我认为这将是一个学习语言的好项目。

我正在构建一个表单以允许玩家为 MWO 配置机甲,并且进展顺利,但我的代码变得越来越混乱,因为我将所有内容都放在一个文件中。我想做的是将我的变量分离到它们自己的 .cs 文件中,并将不同的机甲也分离到它们自己的文件中。

我遇到的问题是,当我将变量移动到其自己文件中的新类时,即使我将类和所有变量设置为公共,其余代码也无法再看到变量。我不确定我做错了什么,我确定这是我忘记的相当简单的事情,但对于我的生活,我无法弄清楚我做错了什么。

public static class MechCommon_Fields
{
    public const int NUMBER_OF_EQUIPABLE_SECTIONS = 6;
    public const int NUMBER_OF_CRITICAL_SECTIONS = 8;
    public const int NUMBER_OF_SECTIONS = 11;
    public const int UNKNOWN_LOCATION = -1;
    public const int RIGHT_ARM = 0;
    public const int LEFT_ARM = 1;
    public const int RIGHT_TORSO = 2;
    public const int LEFT_TORSO = 3;
    public const int CENTER_TORSO = 4;
    public const int HEAD = 5;
    public const int RIGHT_LEG = 6;
    public const int LEFT_LEG = 7;
    public const int RIGHT_REAR_TORSO = 8;
    public const int LEFT_REAR_TORSO = 9;
    public const int CENTER_REAR_TORSO = 10;
    public const int BOTH_SIDE_TORSOS = 11;
    public const int BOTH_ARMS = 12;
    public const int NUMBER_OF_HARDPOINT_TYPES = 4;
    public const int BALLISTIC = 0;
    public const int ENERGY = 1;
    public const int MISSILE = 2;
    public const int AMS = 3;
    public const int OMNI = 4;
    public const int AMMO = 3;
    public const int OTHER = 4;
    public const int SELECTED = 5;
    public const double SRM_DAMAGE = 2.5;
    public const double LRM_DAMAGE = 1.8;
    public const double SRM_RECYCLE = 3.5;
    public const double LRM_RECYCLE = 3.25;
    public const double SRM_DELAY = 0.25;
    public const double LRM_DELAY = 0.5;
    public const double LRM_IMPULSE = 0.8;
    public const int SRM_RANGE = 270;
    public const int MRM_RANGE = 450;
    public const int LRM_MIN_RANGE = 180;
    public const int ENHANCED_LRM_MIN_RANGE = 90;
    public const int LRM_RANGE = 630;
    public const int LRM_MAX_RANGE = 1000;
    public const int EXTENDED_LRM_MIN_RANGE = 300;
    public const int EXTENDED_LRM_RANGE = 1140;
    public const int EXTENDED_LRM_MAX_RANGE = 1140;
    public const int SRM_SPEED = 300;
    public const int STREAK_SRM_SPEED = 200;
    public const int LRM_SPEED = 100;
    public const int ARTEMIS_IV_CRITICALS = 1;
    public const int ARTEMIS_IV_COST = 1;
    public const double ARTEMIS_IV_TONNAGE = 1.0;
    public const int LASER_RANGE_MODIFIER = 2;
    public const int PROJECTILE_RANGE_MODIFIER = 3;
    public const int INTERNALS = 0;
    public const int ARMOR = 1;
    public const int INTERNALS_TOTAL = 8;
    public const int ARMOR_TOTAL = 8;
    public const int NUMBER_OF_MAIN_SECTION = 6;
    public const int NUMBER_OF_LESSER_SECTION = 3;
    public const int NUMBER_OF_MAIN_SECTION_CRITICALS = 12;
    public const int NUMBER_OF_LESSER_SECTION_CRITICALS = 6;
    public const int BALLISTIC_MAX_RANGE_MODIFIER = 3;
    public const int ENERGY_MAX_RANGE_MODIFIER = 2;
    public const int LOWER_ARM_ACTUATOR = 0;
    public const int HAND_ACTUATOR = 1;
    public const int UNKNOWN_ITEM_TYPE = 0;
    public const int COMPONENT_ITEM_TYPE = 1;
    public const int WEAPON_ITEM_TYPE = 2;
    public const int AMMO_ITEM_TYPE = 3;
    public const int EQUIPMENT_ITEM_TYPE = 4;
    public const int HEAT_SINK_ITEM_TYPE = 5;
    public const int JUMP_JET_ITEM_TYPE = 6;
    public const int ARMOR_ITEM_TYPE = 7;
    public const int INTERNAL_ITEM_TYPE = 8;
    public const int CASE_ITEM_TYPE = 9;
    public const int ENGINE_ITEM_TYPE = 10;
    public const int OTHER_ITEM_TYPE = 11;
    public const int TORSO = 0;
    public const int ARM = 1;
    public const int NUMBER_OF_MOVING_SECTIONS = 2;
    public const int YAW = 0;
    public const int PITCH = 1;
    public const int AXIS_OF_MOVEMENT = 2;
    public const double DOUBLE_HEAT_SINK_DISSIPATION = 1.4;
}

}

我试过这样调用变量:NUMBER_OF_EQUIPABLE_SECTIONS = 10;

和:

MechCommon_Fields.NUMBER_OF_EQUIPABLE_SECTIONS = 10;

两者都不起作用,我非常难过。

感谢您能给我的任何见解。

好的,这是我遇到的问题的更好和更新的示例。

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MechLab
{
    public class Mechs
    {
        public int enginePower;
        public int tonnage;
        public double maxEngineRating;
        public int stockEngineRating;
        public string weightClass;
        public double maxSpeed;

        public string mechChassis;
        public string mechVariant;
        public double speed;
        public long basecost;

        public int ballisticHP;
        public int missileHP;
        public int energyHP;
        public int amsHP;
        public int omniHP;

        public int armorHead;
        public int armorCT;
        public int armorRT;
        public int armorLT;
        public int armorRCT;
        public int armorRRT;
        public int armorRLT;
        public int armorRA;
        public int armorLA;
        public int armorRL;
        public int armorLL;
        public int totalArmor;
    }
}

现在,我在另一个文件中有一个方法,它将引用其中一些变量以将它们显示在表单中。

public void loadMech()
        {
            maxSpeed = ((stockEngineRating / tonnage) * 16.2);

            txtMechChassis.Text = mechChassis;
            txtMechVariant.Text = mechVariant;
            txtSpeed.Text = maxSpeed.ToString();
            txtTonnage.Text = tonnage.ToString();


        }

我得到每个变量的“在当前上下文中不存在”错误。

4

5 回答 5

1

您可以使用部分类。更多细节在这里

使用部分类,您可以像往常一样使用您的变量、常量等,即使它在单独的文件中定义。

于 2013-03-28T05:21:33.757 回答
1

如果Mech_CommonFields在与您尝试使用它的类不同的命名空间中定义,则需要在文件using顶部添加指令.cs

using MyProject.Namespace; // replace with your namespace
于 2013-03-28T05:26:26.017 回答
1

如果您发布的类Mech_CommonFields

public class Mech_CommonFields
{

    public int NumberOfEquipableSections { get; set; }
    public int NumberOfSections { get; set; }
    public int UnknownLocation { get; set; }
    public int RightArm { get; set; }
    public int LeftArm { get; set; }
    public int RightTorso { get; set; }
    public int LeftTorso { get; set; }
    public int CenterTorso { get; set; }
    // etc.
}

然后你可以创建一个具有特定类型属性的新机甲,如下所示:

Mech_CommonFields myMech = new Mech_CommonFields() { 
                               NumberOfEquipableSections = 6,
                               NumberOfSections = 11,
                               UnknownLocation = -1 };

自动属性(如上所示)允许您在实例化它时使用您想要的值初始化类,只需在实例化对象时将值分配给您想要的属性(如第二个代码示例中所示)。这具有与 IntelliSense 良好配合的额外好处 - 当您在花括号内时,它将弹出一个属性列表供您使用,并且只会显示您尚未用于该实例的属性)。

如果您需要在运行时为字段分配值,则使用const没有任何意义,因为无法修改常量。

同样,虽然静态允许您在程序运行期间随时更改值,但它会更改引用它们的所有对象的值。换句话说,如果您有 3 个机甲,并将其中一个的 NUMBER_OF_EQUIPABLE_SECTIONS 的值设置为 12,那么所有机甲的值都将设置为 12(如果字段/属性被标记为静态)。

同样,不确切地知道你是如何使用你的课程的,以及程序的其余部分是什么样子的,很难给出一个好的答案,但这有望让你朝着正确的方向前进。

有关更多信息,请参阅这些链接:

自动实现的属性

如何:使用对象初始化器初始化对象

常量

静止的

于 2013-03-28T05:45:16.820 回答
0

You cant assign values to constants:

MechCommon_Fields.NUMBER_OF_EQUIPABLE_SECTIONS = 10;
于 2013-03-28T05:34:41.973 回答
0

您可以将静态放在每个变量之前。

有点像这样:

public static int AMMO = 3;

这应该有助于解决问题。

于 2013-03-28T05:27:13.520 回答