我有這樣越來越溢出異常,而在C#構造函數intiliazing
public class Menu
{
public int MenuID { get; set; }
public string MenuName { get; set; }
public string LinkAddress { get; set; }
public Menu[] menu;
}
一類,而我試圖在構造函數中intilize如下我得到一個溢出異常錯誤
public Menu() {
Menu[] menu = {
new Menu { MenuID = 1, MenuName = "Home", LinkAddress = "home/index" }
, new Menu { MenuID = 2, MenuName = "Gallery", LinkAddress = "Gallery/index" }
, new Menu { MenuID = 3, MenuName = "Academics", LinkAddress = "Academics/index" }
, new Menu { MenuID = 4, MenuName = "Blog", LinkAddress = "Blog/index" }
, new Menu { MenuID = 5, MenuName = "Login", LinkAddress = "Login/index" }
, new Menu { MenuID = 6, MenuName = "UserLogin", LinkAddress = "UserLogin/index" }
, new Menu { MenuID = 7, MenuName = "ForgotPassword", LinkAddress = "ForgotPassword/index" }
};
}
我也試圖初始化myvariable
public Menu[] menu;
在構造函數,但我無法intialize我噸。任何人都可以告訴我究竟我做錯了什麼。我認爲構造函數用於初始化變量,但我無法使用它。 感謝您的幫助。
StackOverflowException!= OverflowException。您的默認構造函數創建具有默認構造函數的菜單項。它使用默認構造函數創建菜單項。它使用默認構造函數創建菜單項。它使用默認構造函數創建菜單項。它使用默認構造函數創建菜單項。它用默認的構造函數創建菜單項.... Kaboom!考慮你需要*兩個*類,一個Menu和一個MenuItem類。 –