【发布时间】:2017-02-25 17:05:24
【问题描述】:
我正在使用静态类作为某种库(也许不是最好的主意,但我真的不知道我在做什么......)并且我有一些带有对象的字段,所以我可以访问它们很容易被名字。但是我仍然需要一个数组,我可以使用一种方法来搜索。
但是当我调用该方法 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;
}
}
非常感谢您抽出宝贵时间!
【问题讨论】:
-
由于您的集合初始化程序,您的数组正在使用空引用构造。将数组构造移到静态构造函数中。
-
@Svek 我没有复制所有内容,如果你的意思是第一行,那就是数组
-
你必须使用
allTypes = new IndustryType[] { blah blah }; -
除非你有充分的理由,否则不要使用
static。见When to use static classes in C#。 -
我同意他应该删除这个静态类。但是他应该保留静态字段(由属性封装)并将它们移动到它们所属的非多元化类中。一个名为 AllTypes 的静态属性肯定是有意义的(尽管最好使用一个列表来实现,该列表具有由类的实例构造函数添加到它的项目)。单例是最常被鄙视的反模式之一。它们有其用途,但这是一个笼统的说法。静态类对于实用程序类(如 IO 类)非常有用,但也可用于通过嵌套静态类进行缓存。
标签: c#