我正在使用静态类作为某种库(可能不是最好的主意,但我真的不知道我在做什么......)并且我有一些带有对象的字段,因此我可以通过名称轻松访问它们. 但我仍然需要一个数组,我可以使用一种方法进行搜索。
但是当我调用该方法 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] 删除。
我来说两句