使用 C# 中已经存在的对象创建静态数组

安东·J·哈坎森

我正在使用静态类作为某种库(可能不是最好的主意,但我真的不知道我在做什么......)并且我有一些带有对象的字段,因此我可以通过名称轻松访问它们. 但我仍然需要一个数组,我可以使用一种方法进行搜索。

但是当我调用该方法 GetTypeByName("Iron Mine") 时,它应该返回查看数组并返回 ironMine 对象,但它只是尖叫“对象引用未设置为对象的实例”......我很清楚缺少关于静态数组或数组的一些东西,我还不太习惯它们......

public static class IndustryTypes
{
    public static IndustryType[] allTypes = //This is that array that seems to mess things up...
    {
        ironMine, coalMine, aluminiumMine, copperMine, uraniumMine, goldMine,
        quarry, oilWell,
        farm
    };

    public static IndustryType noType;
    public static IndustryType ironMine, coalMine, aluminiumMine, copperMine, uraniumMine, goldMine;
    public static IndustryType quarry, oilWell;
    public static IndustryType farm;

    static IndustryTypes()
    {
        noType = new IndustryType("noType", 0, Color.Red);
        ironMine = new IndustryType("Iron Mine", 50000, Game.color_mine);
        coalMine = new IndustryType("Coal Mine", 40000, Game.color_mine);
        aluminiumMine = new IndustryType("Aluminium Mine", 100000, Game.color_mine);
        copperMine = new IndustryType("Copper Mine", 55000, Game.color_mine);
        uraniumMine = new IndustryType("Uranium Mine", 150000, Game.color_mine);
        goldMine = new IndustryType("Gold Mine", 125000, Game.color_mine);

        quarry = new IndustryType("Quarry", 25000, Game.color_mine);
        oilWell = new IndustryType("Oil Well", 110000, Game.color_oil);
        farm = new IndustryType("Farm", 10000, Game.color_farm);

    }

    public static IndustryType GetTypeByName(string name)
    {
        for (int i = 0; i < allTypes.Length; i++)
        {
            if (name == allTypes[i].name) //This says "Object reference not set to an instance of an object" or something like that...
            {
                return allTypes[i];
            }
        }
        return noType;
    }
}

这一切都在一个名为“IndustryTypes”的静态类中,所以我不需要实例化它。

“IndustryType”类是一个非静态类。

问你是否不明白我的意思......我真的不了解自己,但我会尝试!:D

这是“IndustryType”类:

public class IndustryType
{
    public string name;
    public int cost;
    public Color color;
    public List<Resource> components;
    public List<Resource> products;

    public IndustryType(string _name, int _cost, Color _color)
    {
        name = _name;
        cost = _cost;
        color = _color;
    }
}

非常感谢您抽出宝贵时间!

妮贝瑞

类的静态构造函数IndustryTypes在所有静态字段初始化后执行。所以,目前,当它被执行时:

public static IndustryType[] allTypes = //This is that array that seems to mess things up...
{
    ironMine, coalMine, aluminiumMine, copperMine, uraniumMine, goldMine,
    quarry, oilWell,
    farm
};

诸如此类的所有字段ironMine仍未初始化并指向 null。

解决方案:将字段的初始化从静态构造函数移到声明本身。还要注意,初始化程序是按文本顺序执行的,因此您必须在所有其他字段之后最后声明数组。

public static class IndustryTypes
{
    public static IndustryType noType = new IndustryType("noType", 0, Color.Red);
    public static IndustryType ironMine = new IndustryType("Iron Mine", 50000, Game.color_mine);
    public static IndustryType coalMine = new IndustryType("Coal Mine", 40000, Game.color_mine);
    public static IndustryType aluminiumMine = new IndustryType("Aluminium Mine", 100000, Game.color_mine);
    public static IndustryType copperMine = new IndustryType("Copper Mine", 55000, Game.color_mine);
    public static IndustryType uraniumMine = new IndustryType("Uranium Mine", 150000, Game.color_mine);
    public static IndustryType goldMine = new IndustryType("Gold Mine", 125000, Game.color_mine);
    public static IndustryType quarry = new IndustryType("Quarry", 25000, Game.color_mine);
    public static IndustryType oilWell = new IndustryType("Oil Well", 110000, Game.color_oil);
    public static IndustryType farm = new IndustryType("Farm", 10000, Game.color_farm);

    public static IndustryType[] allTypes = {
        IndustryTypes.ironMine,
        IndustryTypes.coalMine,
        IndustryTypes.aluminiumMine,
        IndustryTypes.copperMine,
        IndustryTypes.uraniumMine,
        IndustryTypes.goldMine,
        IndustryTypes.quarry,
        IndustryTypes.oilWell,
        IndustryTypes.farm
    };
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在vector中使用现有对象;如果C ++中不存在,则创建新对象

c ++在函数中创建对象数组需要在全局范围内使用该数组

使用C ++中的对象功能创建线程

使用RapidJSON解析C ++中的对象数组

C ++中对象数组的静态内存分配

使用“新”运算符在数组C ++中创建对象

C#使用字符串数组中的嵌套对象动态创建JSON

如何使用JSON.Net从C#中的JSON对象创建数组

创建使用数组对象C#的Session Helper类

C# 使用数组作为基础创建对象?

有什么理由在C ++中为布尔使用枚举而不是已经存在的布尔值吗?

如果键有效,如何检查 Json 文件并使用 RapidJson 在 C++ 中创建对象数组

使用C预处理器根据以后的扩展创建静态大小的数组

在C中使用静态数组存储数据

如何在C中创建对象数组?

在C#中创建和使用PowerShell对象引用

使用nlohmann json在c ++中创建嵌套的json对象

使用C#中的RSA私钥文件创建RSACryptoServiceProvider对象

使用for循环在PHP中创建对象数组

使用数组中的值创建对象

使用数组中的键值对创建对象

使用数组在类中创建对象

使用传入的参数在C ++中创建3D数组

使用C中的指针创建/打印数组

如果数组存在,则将对象推入数组,否则在MongoDB中使用对象创建数组

使用ruby FFI在ruby中创建静态数组类

创建对象数组c ++

使用g ++ -std = c ++ 11使用模板创建静态库

使用C#如果数组中存在字符串,则获取数组中的索引