[英]using variables and methods from separate .cs files in c#
我對 c# 非常陌生,我正在嘗試構建一個小應用程序來幫助一些玩游戲的朋友。 我認為這將是一個學習語言的好項目。
我正在構建一個表單,允許玩家為 MWO 配置機甲,並且進展順利,但我的代碼變得越來越混亂,因為我將所有內容都放在一個文件中。 我想做的是將我的變量分離到它們自己的 .cs 文件中,並將不同的機械也分離到它們自己的文件中。
我遇到的問題是,當我將變量移動到它自己文件中的新類時,即使我將類和所有變量設置為 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;
}
}
我試過這樣調用變量:NUMBER_OF_EQUIPABLE_SECTIONS = 10;
和:
MechCommon_Fields.NUMBER_OF_EQUIPABLE_SECTIONS = 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_CommonFields
定義在與您嘗試使用它的類不同的命名空間中,則需要在.cs
文件的頂部添加using
指令。
using MyProject.Namespace; // replace with your namespace
如果您發布的類 ( Mech_CommonFields
) 旨在定義任何給定機械上的所有組件,無論類型如何,並且實際值可以在類型之間更改,我將使用自動屬性,如下所示:
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 個機甲,並將其中一個的 NUMBER_OF_EQUIPABLE_SECTIONS 的值設置為 12,則所有機甲都將設置為 12(如果字段/屬性被標記為靜態)。
同樣,不知道你是如何使用你的課程以及程序的其余部分是什么樣的,很難給出一個好的答案,但這有望讓你朝着正確的方向前進。
有關更多信息,請參閱這些鏈接:
您可以在每個變量之前放置靜態。
有點像這樣:
public static int AMMO = 3;
這應該有助於解決問題。
您不能為常量賦值:
MechCommon_Fields.NUMBER_OF_EQUIPABLE_SECTIONS = 10;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.