0

i have a class like this

public class Menu
 {
 public int MenuID { get; set; }
 public string MenuName { get; set; }
 public string LinkAddress { get; set; }
 public Menu[] menu;
}

while i am trying to intilize in the constructor as follows i get an overflow exception error

 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" } 
 };
 }

i also tried to intialize myvariable

public Menu[] menu;

in the constructor but i was not able to intialize it. can anyone tell me what exactly am i doing wrong. I thought constructor is used to intialize variables but i am not able to intiliaze it. thank you for your help.

asked Jul 11, 2016 at 14:24
1
  • StackOverflowException != OverflowException. Your default constructor creates Menu items with the default constructor. Which creates Menu items with the default constructor. Which creates Menu items with the default constructor. Which creates Menu items with the default constructor. Which creates Menu items with the default constructor. Which creates Menu items with the default constructor.... Kaboom! Consider that you need two classes, a Menu and a MenuItem class. Commented Jul 11, 2016 at 15:50

1 Answer 1

1

you are made a circular reference when declaring an array of a class inside the class:

 public Menu[] menu;//<-- declaring inside Menu class produces circular refernce

thus an overflow exception

Use a method instead, e.g:

public void InitMenu()
 {
 this.menu = new 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" }
 };
 }

and consume as:

Menu m = new Menu();
 m.InitMenu();
 m.menu=..;//<--acces your array
answered Jul 11, 2016 at 14:25
Sign up to request clarification or add additional context in comments.

3 Comments

well i want to intilize public Menu[] menu variable, how can i do that
declare it in the class that consumes it
is there no way where i an intilize the variable while i create an object of Menu class...thanks

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.