简体   繁体   中英

Add Generic list in a MyClass but how?

How can i add list in a generic class? Firstly My generic Class is that:

[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(list); ----> i don't use theese codes . These error "included some invalid argument" how can you help me? :)

我想这就是您想要的:

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

Generics can simplified be identified by a at the end of the function/class

List<T> //generic type

T represents a type, ie int (primary types) or MyClass (classes)

So

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

is a List of items of the type MyClass

In your case you dont got a generic class, but I think you can make it generic with the following way:

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;
        //(...)
    }
}

and then call it

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

which creates a new object of your class with a List of FrequencyDays

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM