[英]Type Initialization Failed In Non-Static Class
我遇到了一个棘手的问题,需要为我的一个大型项目解决。 我正在尝试从非静态类引用静态变量(由静态类拥有)。 当我尝试执行此操作时,它将引发异常“'Eternal_Continent.PSTATS'的类型初始值设定项引发了异常”。
内部异常读取
你调用的对象是空的。
这是我的PSTATS课
public static class PSTATS
{
static Locations Locations;
public static string name = "";
public static int health = 100;
public static int dmg = 1;
public static int mana = 100;
public static int hpotion = 3;
public static int mpotion = 3;
public static int def = 1;
public static int level = 1;
public static int xp = 0;
public static float tradereward = 1.0f;
public static string employer = "Knight Artemis";
public static misc.Quest currentquest;
public static misc.NPC currentnpc = null;
public static int npcindex = 0;
public static misc.Location currentlocation = Locations.Ardimir;
public static string reward = "Charisma + 1";
public static bool finding = false;
public static string questreward = "G";
public static int kills = 0;
public static int gold = 0;
// Has found item
public static bool found = false;
//Has job
public static bool job = false;
public static int reqkills = 0;
public static int reqgold = 0;
public static int dex = 1;
public static int str = 1;
public static int itl = 1;
public static int cha = 1;
public static bool existingdialog = false;
}
这是调用它的非静态类
public class Weapon
{
#region
public Weapon(string name, string desc, int dmg, int mana)
{
Name = name;
Desc = desc;
Dmg = Convert.ToInt32(dmg * (PSTATS.level / 0.9));
Manausage = mana;
}
public int Manausage;
public int manausage
{
get
{
return Manausage;
}
set
{
Manausage = value;
}
}
public string Name;
public string name
{
get
{
return Name;
}
set
{
Name = value;
}
}
public string Desc;
public string desc
{
get
{
return Desc;
}
set
{
Desc = value;
}
}
public int Dmg;
public int dmg
{
get
{
return Dmg;
}
set
{
Dmg = Convert.ToInt32(value * (PSTATS.level / 0.9));
}
}
#endregion
}
感谢任何可以帮助我解决此问题的人。
我复制了创建两个类的确切代码。
然后,我在main中创建了一个对象:Weapon w = new Weapon(“ n”,“ d”,6,3); 它是成功的,并且使用静态PSTATS变量没有任何问题。
我所做的唯一更改是删除了PSTAT中的Locations和misc变量,这些变量是创建武器不需要的,并允许我运行测试。
我认为我们仍然缺少您的一些代码,但是我的直觉告诉我,这与您的PSTATS类中的第一个称为Locations
成员有关。 看来Locations
是一个枚举,这意味着您不需要引用它。 尝试删除该成员,然后发布与misc
和Locations
枚举有关的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.