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

C# 在.cs文件之间使用变量和常量,c#,variables,constants,C#,Variables,Constants,我对C#和一般编程非常陌生,我在使用单独的.cs文件中的变量和常量时遇到了问题。我以前问过这个问题,得到了一些答案,但我想我问错了,因为没有一个答案真正回答了我的问题。回答得好我想我只是提出了一个错误的问题。又来了 我有一个类,它包含一个常量列表,这些常量在一个名为Common.cs的文件中永远不会更改,并且这些常量需要在其他文件中引用,如arms.cs文件。我遇到的问题是,我总是收到一个错误,指出常量/变量“在当前上下文中不存在”。我不确定我做错了什么或错过了什么,但我确定这很简单。以下是迄今

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

我有一个类,它包含一个常量列表,这些常量在一个名为Common.cs的文件中永远不会更改,并且这些常量需要在其他文件中引用,如arms.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;
    }
}
这是我的武器

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;

        }
    }
}

我看不出它有什么问题,但作为一名真正的编码新手,我确信我遗漏了一些非常明显的东西。

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

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
s可能会更好。它们将在编译时获得赋值,但您实际上不需要知道它们


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

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

e、 g:

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

MountWeapon(Location.RIGHT_ARM, HardpointType.BALLISTIC);
容易理解比简单

MountWeapon(1, 0);

哇,格式真的搞砸了。有没有办法解决这个问题?请允许我指出,将所有常量放在一个类中只是一个坏主意。至少,某个类中应该只有相关常量,千万不要混淆。Cory完美地回答了你的问题,但我只是想说,我觉得你的类处理方法可能会更好。您应该使用继承自
武器
类的类
AC10
,而不是创建
AC10
的方法。您可以在基类
武器
中定义属性和常量,所有从基类派生的武器都可以在其构造函数中设置自己的默认值。你仍然可以将武器储存在
武器
的集合中,等等。我必须同意John Willemse的观点,我甚至会更进一步,将你的AC10作为武器对象而不是单独的类。它甚至可能值得一想的原型模式啊哈!我知道这很简单。非常感谢你们的建议和帮助!是的,我将每个武器移动到它自己的类中,并将常量移动到一个更有组织的可读类结构中。再次感谢!我知道我可以定义枚举中的计数以什么数字开始,但我可以从-1开始吗?
MountWeapon(1, 0);