Reputation: 23
I am very new to C# and programming in general and I am having an issue using variables and constants from separate .cs files. I have asked this question before and got some answers but I think I asked it wrong because none of the answers actually answered my question. Good answers I think I just presented the question wrong. So here goes again.
I have a class that contains a list of constants that will never change in a file called Common.cs and these constants need to be referenced in other files like a Weapons.cs file. The problem I'm running into is that I am always getting an error stating that the const/variable "does not exist in the current context". I'm not sure what I'm doing wrong or missing but I'm sure it's something simple. Here is an example of my code so far:
here's the class with the constants in it:
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;
}
}
and here's my 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;
}
}
}
I don't see anything wrong with it, but being really new to coding I'm sure I'm missing something fairly obvious.
Upvotes: 0
Views: 1834
Reputation: 107536
Your constants are in the Common
class, so you need to include the class name when you reference them from your Weapon
class.
void AC10()
{
equipmentName = "Autocannon 10";
shortName = "AC10";
crits = 7;
cost = 400000;
hardpointType = Common.BALLISTIC;
minRange = 0;
maxRange = effectiveRange * Common.PROJECTILE_RANGE_MOD;
effectiveRange = 450;
}
As others have been saying, some of your constants -- the "type" constants that you aren't going to really require values for -- might be better off as enum
s as in Marco's answer. They will get assigned values at compile time but you don't really need to know them.
If you have constants that you are going to use the values of, then keep them as such. I would also group them into different classes. There seems to be some fundamentally different sets of constants that you currently have separated by whitespace that could get moved to their own classes. Having descriptive class names will improve the readability and the ability to understand your code.
Upvotes: 6
Reputation: 2644
for some constants i'd suggest grouping them into enums. this would increase readability of your code
e.g.:
public enum Location
{
UNKNOWN,
RIGHT_ARM,
LEFT_ARM,
RIGHT_TORSO,
LEFT_TORSO,
CENTER_TORSO,
[...]
}
public enum HardpointType
{
BALLISTIC,
ENERGY,
MISSILE,
AMS,
OMNI
}
this will proof quite useful later on since i'd consider
MountWeapon(Location.RIGHT_ARM, HardpointType.BALLISTIC);
is easier to understand than
MountWeapon(1, 0);
Upvotes: 1