如何将对象列表添加到类 [英] How to Add List of object to a class

查看:86
本文介绍了如何将对象列表添加到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何将对象列表(例如,列表包含菜单项)添加到类列表功能.

问候,
Sham

Hello All,

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

Regards,
Sham

推荐答案

请参见下面的代码,这是您可以拥有自己的类项目的列表的方法.

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


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


如果您要将一个集合添加到另一个集合(例如两个List集合),则只需执行以下操作:

If you''re adding one collection to another collection (two List collections for einstance) you just need to do this:

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.


这篇关于如何将对象列表添加到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆