Reputation: 23
I am incredibly new to c# and I am trying to build a small app to help out some friends playing a game. I thought this would be a good project to learn the language.
I am building a form to allow players to configure mechs for MWO and it's going well but my code is getting progressively messier ans I have it all in one file. What I'd like to do is to separate my variables into their own .cs file and separate the different mechs into their own files as well.
The issue I'm running to is that when I move the variables to a new class in its own file the rest of the code can no longer see the variables even when I set the class and all of the variables to public. I'm not sure what I'm doing wrong and I'm sure it's something fairly simple that I'm forgetting, but for the life o me I can't figure out what I'm doing wrong.
public static class MechCommon_Fields
{
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_IV_CRITICALS = 1;
public const int ARTEMIS_IV_COST = 1;
public const double ARTEMIS_IV_TONNAGE = 1.0;
public const int LASER_RANGE_MODIFIER = 2;
public const int PROJECTILE_RANGE_MODIFIER = 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_MODIFIER = 3;
public const int ENERGY_MAX_RANGE_MODIFIER = 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;
}
}
I've tried calling the variables like this: NUMBER_OF_EQUIPABLE_SECTIONS = 10;
and:
MechCommon_Fields.NUMBER_OF_EQUIPABLE_SECTIONS = 10;
neither works and I'm terribly stumped.
Thanks for any insight you can give me.
Ok, here's a better and more up-to-date example of the issues I'm having.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MechLab
{
public class Mechs
{
public int enginePower;
public int tonnage;
public double maxEngineRating;
public int stockEngineRating;
public string weightClass;
public double maxSpeed;
public string mechChassis;
public string mechVariant;
public double speed;
public long basecost;
public int ballisticHP;
public int missileHP;
public int energyHP;
public int amsHP;
public int omniHP;
public int armorHead;
public int armorCT;
public int armorRT;
public int armorLT;
public int armorRCT;
public int armorRRT;
public int armorRLT;
public int armorRA;
public int armorLA;
public int armorRL;
public int armorLL;
public int totalArmor;
}
}
Now, I have a method in another file that will reference some of those variable to display them in a form.
public void loadMech()
{
maxSpeed = ((stockEngineRating / tonnage) * 16.2);
txtMechChassis.Text = mechChassis;
txtMechVariant.Text = mechVariant;
txtSpeed.Text = maxSpeed.ToString();
txtTonnage.Text = tonnage.ToString();
}
I get "does not exist in the current context" error for every variable.
Upvotes: 2
Views: 4390
Reputation: 9084
You could put static before each variable.
Kind of like this:
public static int AMMO = 3;
This should help fix the issue.
Upvotes: 0
Reputation: 28540
If the class you posted (Mech_CommonFields
) is designed to define all the components on any given mech, regardless of the type, and the actual values can change between the types, I would use automatic properties, like this:
public class Mech_CommonFields
{
public int NumberOfEquipableSections { get; set; }
public int NumberOfSections { get; set; }
public int UnknownLocation { get; set; }
public int RightArm { get; set; }
public int LeftArm { get; set; }
public int RightTorso { get; set; }
public int LeftTorso { get; set; }
public int CenterTorso { get; set; }
// etc.
}
Then you could create a new mech with the attributes for that particular type like this:
Mech_CommonFields myMech = new Mech_CommonFields() {
NumberOfEquipableSections = 6,
NumberOfSections = 11,
UnknownLocation = -1 };
Automatic properties (as demonstrated above) allow you to intialize the class with the values you want when you instantiate it by simply assigning values to the properties you want when you instantiate the object (as in the second code example). This has the added benefit of working nicely with IntelliSense - it will pop up a list of properties for you to use when you're inside the curly braces, and will only show the ones that you have not yet used for that instance).
Using const
doesn't make any sense if you're ever going to need to assign values to the fields at runtime, as constants can't be modified.
Likewise, while static would allow you to change the values at any time during the program's run, it would change the values for all the objects referring to them. In other words, if you had 3 mechs, and set the value of NUMBER_OF_EQUIPABLE_SECTIONS to 12 for one of them, it would be set to 12 for all of them (if the field/property was marked as static).
Again, not knowing exactly how you are using your class and what the rest of the program looks like it's kind of hard to give a good answer, but this will hopefully get you going in the right direction.
See these links for more information:
How to: Initialize Objects by Using an Object Initializer
Upvotes: 2
Reputation: 65742
You cant assign values to constants:
MechCommon_Fields.NUMBER_OF_EQUIPABLE_SECTIONS = 10;
Upvotes: 0