-4

So if I have this:

public MainMenuModel(string transKey, string stateName, string displayUrl, bool hasSubMenu= false,List<SubMenuModel>subMenu=null)
{
            TransKey = transKey;
            StateName = stateName;
            DisplayUrl = displayUrl;          
            HasSubMenu = hasSubMenu;
            SubMenu = subMenu;             
}
        public string TransKey { get; set; }
        public string StateName { get; set; }
        public string DisplayUrl { get; set; }

        public bool HasSubMenu { get; set; }

        public List<SubMenuModel>SubMenu { get; set; }

}



public class SubMenuModel
{
            public SubMenuModel(string transKey, string stateName, string displayUrl)
            {
                TransKey = transKey;
                StateName = stateName;
                DisplayUrl = displayUrl;              
            }
            public string TransKey { get; set; }
            public string StateName { get; set; }
            public string DisplayUrl { get; set; }


}

How can I add SubMenu in MainMenu with some condition for example:

if(test!=null)
{
    SubMenu.Add(new SubMenuModel("PERSONAL_INFORMATION","account.personalinformation","/account/personalinformation"));
}
SubMenu.Add(new SubMenuModel("NOTIFICATIONS", "account.notificationsettings", "/account/notifications"));
SubMenu.Add(new SubMenuModel("CHANGE_PASSWORD", "account.changepassword", "/account/passwordchange"));
SubMenu.Add(new SubMenuModel("GAME_SETTINGS", "default", "default"));

MainMenu.Add(SubMenu) --> this is not working...how can I add with condition sub menu in main menu? I tried MainMenu.AddRange(SubMenu) also but I cant do like that because its different types. I tried something like this :

for (int i = 0; i < SubMenu.Count; i++)
{
   MainMenu[0].SubMenu.Add(SubMenu[i]);
}

But I get an error.Any suggestion?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
None
  • 8,817
  • 26
  • 96
  • 171
  • 1
    "But I get an error" is *never* enough information. What error? (Also, please format your code. You've asked 84 questions now, so you really should know how to present code in a readable way...) – Jon Skeet Jul 16 '15 at 09:30
  • I'm confused... You say "`MainMenu.Add(SubMenu)` --> this is not working but I can't actually see that line of code anywhere in the code you have given us... Also you seem to be adding submenu items conditionally already with your `if (test!=null)[...]` part... – Chris Jul 16 '15 at 09:33
  • im trying to add them but i get an error of null object reference – None Jul 16 '15 at 09:33
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Chris Jul 16 '15 at 09:34
  • @Chris if u know anything u will know that is not the main problem ;) – None Jul 16 '15 at 09:36
  • I do know things. I know you just said "I get an error of null object reference". How then is that not the main problem? Either you know how to fix it in which case why haven't you fixed it or you don't in which case that is your current most pressing problem. – Chris Jul 16 '15 at 09:37
  • i get that error because i dont add properly list into list – None Jul 16 '15 at 09:38
  • No, you get that error because the list you are trying to add to is null. Seriously, go fix your null reference exception and then come back if you still have a problem. – Chris Jul 16 '15 at 09:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83440/discussion-between-none-and-chris). – None Jul 16 '15 at 13:52

2 Answers2

2

Why can't you create the element MainMenu and then add with condition?

MainMenuModel model = new MainMenuModel("TransKey", "StateName", "DisplayUrl");
if(condition) {
  model.SubMenu.Add(new SubMenuModel("GAME_SETTINGS", "default", "default"));
}

Another easy way is to create a method:

public void AddSubMenu(bool condition, SubMenuModel subMenu) {
    if(condition)
        SubMenu.Add(subMenu);
}

Or alternatively you need to add a new property in the SubMenuModel class to put the condition to render it a feature of the model, in example an integer:

public int Condition { get; set; }

Then in the MainMenuModel you can manage the Add method said below like that:

public void AddSubMenu(bool condition, SubMenuModel subMenu) {
    if(condition)
        SubMenu.Add(subMenu);
}

or create a new property only read-only to get only available sub menu:

public List<SubMenuModel> AvailableSubMenu { 
    // Consider sm.Condition is an integer and we want only the positive ones
    get { return SubMenu.FindAll(sm => sm.Condition > 0);
}

In this way you will get only the SubMenuModel that satisfy the condition.

peterboccia
  • 160
  • 8
1

Because in MainMenuModel you already have

public List<SubMenuModel> SubMenu { get; set; }

So, just do it like this

MainMenu[positionOfDataInList].SubMenu = subMenuToAdd;

If you have more SubMenu to add just do

MainMenu[positionOfDataInList].SubMenu.AddRange(anotherSubMenu);
Natechawin
  • 316
  • 1
  • 5
  • i can not acces to property SubMenu from MainMenu ... only can access to property of list such as add add range and things like that – None Jul 16 '15 at 10:01
  • Ok, I think at last I already understand what you're trying to do a little bit. So do you want a new List that can add either MainMenu or SubMenu or you want to add SubMenu in to the member of the List ? if first you need to use base class or interface, if second you gonna do like I reply over there. – Natechawin Jul 16 '15 at 10:11