Click here to Skip to main content
15,921,174 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

How to Add List of object (for eg.List contains menu items) to a class List function.

Regards,
Sham
Posted
Updated 7-Apr-11 1:13am
v2
Comments
Prerak Patel 7-Apr-11 7:13am    
What would you do with that?
dan!sh 7-Apr-11 7:20am    
You need to be clear with your question.

See the code below, This is how you could have a list of your own Class Items.


C#
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();
        }
        List<MenuItem> MyMenuItems = new List<MenuItem>();
        private void Form1_Load(object sender, EventArgs e)
        {
            MyMenuItems.Add(new MenuItem(1, "Sales"));
            MyMenuItems.Add(new MenuItem(2, "Purchase"));
            MyMenuItems.Add(new MenuItem(3, "Stock"));
            MyMenuItems.Add(new MenuItem(4, "Deliveries"));
            MyMenuItems.Add(new MenuItem(5, "Returns"));

        }
    }

    public class MenuItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public MenuItem()
        {
        }
        public MenuItem(int p_id, string p_name)
        {
            Id = p_id;
            Name = p_name;
        }
    }
}
 
Share this answer
 
Comments
sagar wasule 24-Apr-15 5:35am    
Without creating a new object every time can I do the same operation ? Any approach which will enable me to achieve the same result but without creating the object repeatedly.
If you're adding one collection to another collection (two List collections for einstance) you just need to do this:

C#
List<string> myList1 = new List<string>
List<string> myList2 = new List<string>

myList1.Add("one");
myList1.Add("two");
myList1.Add("three");

myList2.AddRange(myList1.ToArray());

If that's not what you want to do, you're going to have to explain your question better. BTW, if that IS what you want to do, both collections should contain compatible objects.
 
Share this answer
 
Comments
[no name] 6-Dec-12 12:19pm    
Can myList1 different in length than myList2? or 1 to receive less arguments that 2?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900