在.cs文件之间使用变量和常量

时间:2013-04-11 14:07:31

标签: c# variables constants

我对C#和编程很新,我遇到了使用来自单独.cs文件的变量和常量的问题。我之前已经问了这个问题并得到了一些答案,但我认为我错了,因为没有一个答案实际上回答了我的问题。好的答案我想我刚才提出错误的问题。所以又来了。

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

这里是包含常量的类:

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;

        }
    }
}

我没有看到它有什么问题,但对编码来说真的很新,我确信我错过了一些相当明显的东西。

2 个答案:

答案 0 :(得分: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;
}

更新

正如其他人一直在说的那样,你的一些常量 - 你不会真正需要的“类型”常量 - 可能会像在Marco的答案中那样更好enum。它们将在编译时获得指定值,但您实际上并不需要了解它们。

如果你有 的常量将使用其值,那么请保留它们。我也会把它们分成不同的类。似乎有一些根本不同的常量集,你现在用空格分隔,可以移动到它们自己的类。具有描述性的类名将提高可读性和理解代码的能力。

答案 1 :(得分:1)

对于某些常量,我建议将它们分组到枚举中。这样可以提高代码的可读性

e.g:

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);