在.cs文件之间使用变量和常量
本文关键字:变量 常量 cs 文件 之间 | 更新日期: 2023-09-27 18:08:44
我是c#和编程的新手,我在使用单独的。cs文件中的变量和常量时遇到了问题。我以前问过这个问题,也得到了一些答案,但我认为我问错了,因为没有一个答案真正回答了我的问题。回答得好,我想我只是提错了问题。又来了。
我有一个类,它包含一个常量列表,将永远不会改变一个文件称为Common.cs和这些常量需要在其他文件,如arms .cs文件引用。我遇到的问题是,我总是得到一个错误,说明const/变量"不存在于当前上下文中"。我不确定我做错了什么或错过了什么,但我确信这很简单。下面是我到目前为止的代码示例:
下面是类的常量:
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;
}
}
和这是我的武器。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;
}
}
}
我看不出有什么问题,但作为一个真正的编码新手,我肯定我错过了一些相当明显的东西。
你的常量在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
s,就像Marco的答案一样。它们会在编译时被赋值,但是你并不需要知道它们。
如果您有要使用的值的常量,那么保持它们不变。我也会把他们分成不同的类别。似乎有一些基本不同的常量集,您目前用空格分隔它们,可以将它们移动到它们自己的类中。具有描述性的类名将提高可读性和理解代码的能力。
对于一些常量,我建议将它们分组到枚举中。这将增加代码的可读性
例如:
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);