[英]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.