C# 在c中使用单独的.cs文件中的变量和方法#

C# 在c中使用单独的.cs文件中的变量和方法#,c#,C#,我对c#非常陌生,我正在尝试构建一个小应用程序来帮助一些玩游戏的朋友。我认为这将是一个学习语言的好项目 我正在建立一个表单,允许玩家为MWO配置机械装置,进展顺利,但我的代码越来越混乱,我将所有这些都放在一个文件中。我想做的是将我的变量分离到它们自己的.cs文件中,并将不同的Mech也分离到它们自己的文件中 我运行的问题是,当我将变量移动到自己文件中的新类时,即使我将该类和所有变量设置为public,代码的其余部分也无法再看到这些变量。我不确定我做错了什么,我肯定这是一件很简单的事情,我忘记了,

我对c#非常陌生,我正在尝试构建一个小应用程序来帮助一些玩游戏的朋友。我认为这将是一个学习语言的好项目

我正在建立一个表单,允许玩家为MWO配置机械装置,进展顺利,但我的代码越来越混乱,我将所有这些都放在一个文件中。我想做的是将我的变量分离到它们自己的.cs文件中,并将不同的Mech也分离到它们自己的文件中

我运行的问题是,当我将变量移动到自己文件中的新类时,即使我将该类和所有变量设置为public,代码的其余部分也无法再看到这些变量。我不确定我做错了什么,我肯定这是一件很简单的事情,我忘记了,但我的生活,我无法找出我做错了什么

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

我尝试过这样调用变量: 可配备的截面数量=10

以及:

这两种方法都不管用,我非常难堪

谢谢你能给我的任何见解

好的,这里有一个关于我所遇到的问题的更好、更新的例子

    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;
    }
}
现在,我在另一个文件中有了一个方法,它将引用其中的一些变量以在表单中显示它们

public void loadMech()
        {
            maxSpeed = ((stockEngineRating / tonnage) * 16.2);

            txtMechChassis.Text = mechChassis;
            txtMechVariant.Text = mechVariant;
            txtSpeed.Text = maxSpeed.ToString();
            txtTonnage.Text = tonnage.ToString();


        }

每个变量都会出现“当前上下文中不存在”错误

您可以使用分部类。更多细节


对于分部类,您可以像往常一样使用变量、常量等,即使它是在单独的文件中定义的。

如果
Mech\u CommonFields
是在不同于您试图使用它的类的命名空间中定义的,您需要在
.cs
文件的顶部添加一个指令

using MyProject.Namespace; // replace with your namespace

您可以将static放在每个变量之前

有点像这样:

public static int AMMO = 3;
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.
}
Mech_CommonFields myMech = new Mech_CommonFields() { 
                               NumberOfEquipableSections = 6,
                               NumberOfSections = 11,
                               UnknownLocation = -1 };

这将有助于解决问题。

您不能为常量赋值:

MechCommon_Fields.NUMBER_OF_EQUIPABLE_SECTIONS = 10;
如果您发布的类(
Mech_CommonFields
)旨在定义任何给定Mech上的所有组件,而不管类型如何,并且实际值可以在类型之间更改,我将使用自动属性,如下所示:

public static int AMMO = 3;
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.
}
Mech_CommonFields myMech = new Mech_CommonFields() { 
                               NumberOfEquipableSections = 6,
                               NumberOfSections = 11,
                               UnknownLocation = -1 };
然后,您可以创建一个新的机械,该机械具有特定类型的属性,如下所示:

public static int AMMO = 3;
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.
}
Mech_CommonFields myMech = new Mech_CommonFields() { 
                               NumberOfEquipableSections = 6,
                               NumberOfSections = 11,
                               UnknownLocation = -1 };
自动属性(如上所示)允许您在实例化类时使用所需的值初始化该类,只需在实例化对象时将值分配给所需的属性(如第二个代码示例所示)。这还有一个与IntelliSense协同工作的额外好处——当您在花括号中时,它会弹出一个属性列表供您使用,并且只显示您尚未在该实例中使用的属性)

如果需要在运行时为字段赋值,则使用
const
没有任何意义,因为常量无法修改

同样,虽然static允许您在程序运行期间随时更改值,但它会更改引用它们的所有对象的值。换言之,如果您有3个机械,并且将其中一个机械部分的数量设置为12,则所有机械部分的数量设置为12(如果字段/属性标记为静态)

同样,不知道你是如何使用你的课程的,以及课程的其他部分看起来是什么样子的,很难给出一个好的答案,但这将有望让你朝着正确的方向前进

有关更多信息,请参见以下链接:


不确定这是否是您的全部代码,但您可以做很多事情。有一点是,您发布的类是静态的,因此要引用其中的字段,您需要执行类似于
Mech\u CommonFields.NUMBER\u OF \u equitable\u SECTIONS
的操作。对于静态类,只需使用类名后跟字段(属性、方法等)。不确定为什么要使用常量,因为我希望这些值在不同的机械之间发生变化(从未玩过游戏,所以不确定)。您已将字段声明为常量,因此无法为其分配新值,只能读取值。@Tim抱歉,Tim,您是对的,但我知道使用静态变量是可行的。我也在同样的情况下工作,我必须从另一个.cs文件中选择不同的文件。@自由职业者-没问题。是的,静态变量会起作用-但重要的是要注意,在这种情况下,这可能不是做事情的最佳方式,因为无论有多少个机械,每个变量都只有一个实例-为一个机械更改它,您就为所有机械更改它。如果您的
loadMech()
方法在不同的类中,那么
mech
,您需要创建类的实例来访问字段,如
Mechs myMech=new Mechs()-然后您可以执行
txtMechChassis.Text=myMech.mechChasis。我突然意识到我发布了旧代码。这些变量不是常量,我原本以为它们可以像模板一样使用,但后来我改变了主意,它们都是同一个名称空间的一部分。对于我尝试引用的每个变量,我只会得到“在当前上下文中不存在”错误。