在MyClass中添加通用列表但是如何?

时间:2009-01-30 08:14:20

标签: c# .net generics

如何在泛型类中添加列表? 首先,我的通用类是:

[Serializable]
    public class ScheduleSelectedItems
    {
        private string Frequency;
        List FrequencyDays = new List();
        private string Time;
        private string StartTime;
        private string EndTime;
        private string StartDate;
        private string EndDate;
        private string Name;


        public ScheduleSelectedItems(string frequency,List frequencydays,
                                     string time, string starttime,
                                     string endtime, string startdate, 
                                     string enddate, string name)
        {
            Frequency = frequency;
            FrequencyDays = frequencydays;
            Time = time;
            StartTime = starttime;
            EndTime = endtime;
            StartDate = startdate;
            EndDate = enddate;
            Name = name;
        }
    }

    [Serializable]
    public class ScheduleSelectedItemsList
    {
        public List Items;

        public ScheduleSelectedItemsList()
        {
            Items = new List();
        }
    }

and i want to add ScheduleSelectedItems into ScheduleSelectedItemsList in form1.cs
Form1.cs codes is here :

private void timer1_Tick(object sender, EventArgs e)
        {
            string saat = DateTime.Now.ToShortTimeString();
            string bugun = DateTime.Today.ToShortDateString();
            ScheduleMng smgr = new ScheduleMng();
            ScheduleItemsList schlist = smgr.LoadXml();
            List list = new List();

            for (int i = 0; i = Convert.ToDateTime(schlist.Items[i].StartDate.ToString())
                     && Convert.ToDateTime(bugun) 

slist.Items.Add(列表); ---->我不使用theese代码。这些错误“包括一些无效的争论”你怎么能帮助我? :)

2 个答案:

答案 0 :(得分:2)

这就是你想要的,我想:

List<ScheduleSelectedItems> list = new List<ScheduleSelectedItems>();

答案 1 :(得分:0)

通过函数/类

的末尾可以简化泛型
List<T> //generic type

T代表一种类型,即int(主要类型)或MyClass(类)

所以

List<MyClass> listOfMyClass = new List<MyClass>();

List

类型的MyClass

在你的情况下,你没有通用类,但我认为你可以通过以下方式使它通用:

public class ScheduleSelectedItems<T>
{
    private string frequency;
    List<T> itemsToSchedule = new List<T>();
    //(...)


    public ScheduleSelectedItems(string frequency,List<T> items, /*(...)*/)
    {
        this.frequency = frequency;
        this.itemsToSchedule = items;
        //(...)
    }
}

然后调用它

ScheduleSelectedItems<FrequencyDays> myItems = new ScheduleSelectedItems<FrequencyDays>("frequency", new List<FrequencyDays>())

使用List FrequencyDays

创建类的新对象

Here ist a MSDN-Article that explain the basics of generics