uzhas
uzhas

Reputation: 925

How to add list into list?

Ok i have this :

if(something!=null)
{
  SubMenu.Add(new SubMenuModel("PERSONAL_INFORMATION","account.personalinformation","/account/personalinformation"));
}
  if(something123!=null)
{        
   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(new MainMenuModel("SETTINGS", "default", "default", true,




           }));

EDIT:

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

How can i add this SubMenu into Main menu because i have condition so if condition is !=null add to list...any sugesstion?

Upvotes: 3

Views: 359

Answers (2)

Natechawin
Natechawin

Reputation: 316

If your MainMenu and SubMenu share same property or method I suggest you to use interface.

    public interface IMenu
    {
        int a { get; set; }
        string b {get; set; }
    }

    public class SubMenu : IMenu
    {
        public int a { get; set; }

        public string b { get; set; }

        public double c { get; set; }
    }

    public class MainMenu : IMenu
    {
        public int a { get; set; }

        public string b { get; set; }

        public string d { get; set; }
    }

So you can use addrange like this

        List<IMenu> menuList = new List<IMenu>();
        List<MainMenu> mainMenuList = new List<MainMenu>();
        List<SubMenu> subMenuList = new List<SubMenu>();

        menuList .AddRange(mainMenuList);
        menuList .AddRange(subMenuList);

Edit::

Suggest by this sentence of you "@Boot750 i have this in main menu public ListSubMenu { get; set; }"

So just do it like this

var mainMenu = new List<MainMenu>();
var subMenuToAdd = new List<SubMenu>().Add(new SubMenu() { .. });

mainMenu.SubMenu = subMenuToAdd;

Upvotes: 0

Paul
Paul

Reputation: 179

you can follow this link.

you have just to do MainMenu.AddRange(SubMenu);

Hope i helps you

EDIT : Ok so you can make this i think You can add MenuItems to existing MenuItem like:

MenuItem addDevice = new MenuItem("Add Device");
addDevice.MenuItems.Add( new MenuItem("Add More .."));
It would be visible like:

submenu

I see this solution here and it works for me.

Upvotes: 4

Related Questions