0

我对 C# 和一般编程非常陌生,并且在使用单独的 .cs 文件中的变量和常量时遇到问题。我以前问过这个问题并得到了一些答案,但我认为我问错了,因为没有一个答案真正回答了我的问题。好的答案我想我只是提出了错误的问题。所以又来了。

我有一个类,其中包含一个常量列表,这些常量在名为 Common.cs 的文件中永远不会更改,并且这些常量需要在其他文件(如 Weapons.cs 文件)中引用。我遇到的问题是我总是收到一个错误,指出常量/变量“在当前上下文中不存在”。我不确定我做错了什么或错过了什么,但我确信这很简单。到目前为止,这是我的代码示例:

这是其中包含常量的类:

namespace MechLab
{
    public static class Common
    {
        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_CRITICALS = 1;
        public const int ARTEMIS_COST = 1;
        public const double ARTEMIS_TONNAGE = 1.0;

        public const int LASER_RANGE_MOD = 2;
        public const int PROJECTILE_RANGE_MOD = 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_MOD = 3;
        public const int ENERGY_MAX_RANGE_MOD = 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;
    }
}

这是我的 Weapons.cs:

namespace MechLab
{
    public class Weapons
    {
        public int tons;
        public int heat;
        public int crits;
        public int minRange;
        public int maxRange;
        public int effectiveRange;
        public string hardpointType;
        public bool artemisCapable;
        public int ammoCount;
        public double damage;
        public double knock;
        public string weaponName;
        public int cost;
        public string equipmentName;
        public string shortName;

        void AC10()
        {
            equipmentName = "Autocannon 10";
            shortName = "AC10";
            crits = 7;
            cost = 400000;
            hardpointType = BALLISTIC;
            minRange = 0;
            maxRange = effectiveRange * PROJECTILE_RANGE_MOD;
            effectiveRange = 450;

        }
    }
}

我看不出有什么问题,但是对于编码来说真的很陌生,我敢肯定我错过了一些相当明显的东西。

4

2 回答 2

6

您的常量在Common类中,因此当您从Weapon类中引用它们时需要包含类名。

void AC10()
{
    equipmentName = "Autocannon 10";
    shortName = "AC10";
    crits = 7;
    cost = 400000;
    hardpointType = Common.BALLISTIC;
    minRange = 0;
    maxRange = effectiveRange * Common.PROJECTILE_RANGE_MOD;
    effectiveRange = 450;
}

更新:

正如其他人所说,你的一些常量——你不会真正需要值的“类型”常量——可能会enum像 Marco 的回答那样更好。它们将在编译时获得分配值,但您实际上并不需要知道它们。

如果您有要使用其值的常量,请保持原样。我也会把他们分成不同的班级。似乎有一些根本不同的常量集,你目前用空格分隔,可以移动到它们自己的类中。具有描述性的类名将提高可读性和理解代码的能力。

于 2013-04-11T14:10:07.933 回答
1

对于一些常量,我建议将它们分组为枚举。这将增加您的代码的可读性

例如:

public enum Location
{
    UNKNOWN,
    RIGHT_ARM,
    LEFT_ARM,
    RIGHT_TORSO,
    LEFT_TORSO,
    CENTER_TORSO,
    [...]
}

public enum HardpointType
{
    BALLISTIC,
    ENERGY,
    MISSILE,
    AMS,
    OMNI
}

这将在以后证明非常有用,因为我会考虑

MountWeapon(Location.RIGHT_ARM, HardpointType.BALLISTIC);

比更容易理解

MountWeapon(1, 0);
于 2013-04-11T14:47:34.810 回答