none
Adding Items to Generic.List RRS feed

  • Question

  • Hi

    I'm have a function that adds items to list contains "Type", "Town", "Time" on loop for 30 days and between a specific time:

        private readonly static List<BattleGroundTimeEntry>TimeEntries = new List<BattleGroundTimeEntry>();
        public static byte BeetweenWarsMinutes = 180;
        
        public enum BattlegroundType
        {
            Occupation,
            Deathmatch
        }
        public enum BattlegroundTown
        {
            Town1,
            Town2,
            Town3
        }
        
        public class BattleGroundTimeEntry
        {
            public BattlegroundType Type { get; set; }
            public BattlegroundTown Town { get; set; }
            public DateTime Time { get; set; }
        }
        
    private static void InitTimeEntries()
    {
        var now = DateTime.Now.Date;
        var endDate = DateTime.Now.AddDays(30);
        while (now < endDate)
        {
            foreach (BattlegroundType type in Enum.GetValues(typeof(BattlegroundType)))
            {
                foreach (BattlegroundTown town in Enum.GetValues(typeof(BattlegroundTown)))
                {
                    TimeEntries.Add(new BattleGroundTimeEntry
                    {
                        Type = type,
                        Town = town,
                        Time = now
                    });
                    now = now.AddMinutes(BeetweenWarsMinutes);
                }
            }
        }
    }

    Now, I don't really know how the output going to be but I used this and did not work out:

    foreach (BattleGroundTimeEntry i in TimeEntries)
    {
            Console.WriteLine(i);
    }

    I think the output should be like this, but I'm not sure:

    Occupation
    Town1
    2/20/2018 3:00:00 AM
    Occupation
    Town2
    2/20/2018 6:00:00 AM
    Occupation
    Town3
    2/20/2018 9:00:00 AM
    // etc...


    What I want to do is to add only a specific items to the list, without this whole each 180 min thing, but keep it looping for 30 days.

    for the following:

    Deathmatch
    Town1
    2/20/2018 3:00:00 PM
    Deathmatch
    Town2
    2/20/2018 6:00:00 PM
    Occupation
    Town3
    2/20/2018 7:00:00 PM
    Deathmatch
    Town3
    2/20/2018 8:00:00 PM
    // reply only those items, goes on for 30 days.
    
    // next day the same items:
    Deathmatch
    Town1
    2/21/2018 3:00:00 PM
    Deathmatch
    Town2
    2/21/2018 6:00:00 PM
    Occupation
    Town3
    2/21/2018 7:00:00 PM
    Deathmatch
    Town3
    2/21/2018 8:00:00 PM

    any help with this please?,

    Thanks in advance .



    • Edited by Zuher Laith Tuesday, February 20, 2018 11:48 PM
    Tuesday, February 20, 2018 11:47 PM

Answers

  • OK Zuher,

    Try this:

    private readonly static List<BattleGroundTimeEntry> TimeEntries = new List<BattleGroundTimeEntry>();
    
    public enum BattlegroundType
    {
        Occupation,
        Deathmatch
    }
    public enum BattlegroundTown
    {
        Town1,
        Town2,
        Town3
    }
    
    public class BattleGroundTimeEntry
    {
        public BattlegroundType Type { get; set; }
        public BattlegroundTown Town { get; set; }
        public DateTime Time { get; set; }
    }
    
    private static void InitTimeEntries()
    {
        var now = DateTime.Now.Date;
        var endDate = DateTime.Now.AddDays(30);
        
        // changed functionality to this:
        DateTime WarTime;
        while (now < endDate)
        {
            WarTime = now.Date.AddHours(15);
            foreach (BattlegroundTown town in Enum.GetValues(typeof(BattlegroundTown)))
            {
                TimeEntries.Add(new BattleGroundTimeEntry
                {
                    Type = BattlegroundType.Deathmatch,
                    Town = town,
                    Time = WarTime
                });
                if (town == BattlegroundTown.Town1)
                    WarTime = WarTime.AddHours(3);
                else
                    WarTime = WarTime.AddHours(1);
            }
            TimeEntries.Add(new BattleGroundTimeEntry
            {
                Type = BattlegroundType.Occupation,
                Town = BattlegroundTown.Town3,
                Time = WarTime
            });
            now = now.AddDays(1);
        }
        
        foreach (BattleGroundTimeEntry i in TimeEntries)
        {
            Console.WriteLine(i.Type.ToString());
            Console.WriteLine(i.Town.ToString());
            Console.WriteLine(i.Time.ToString());
        }
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Zuher Laith Sunday, February 25, 2018 11:34 PM
    Friday, February 23, 2018 6:10 AM
    Moderator

All replies

  • Greetings Zuher.

    I don't really understand the question, but your output should probably be done something like this.

    foreach (BattleGroundTimeEntry i in TimeEntries)
    {
            Console.WriteLine(i.Type.ToString());
            Console.WriteLine(i.Town.ToString());
            Console.WriteLine(i.Time.ToString());
    }


    • Edited by Ante Meridian Wednesday, February 21, 2018 12:21 AM Missed the ToString() bits.
    Wednesday, February 21, 2018 12:21 AM
  • Greetings Zuher.

    I don't really understand the question, but your output should probably be done something like this.

    foreach (BattleGroundTimeEntry i in TimeEntries)
    {
            Console.WriteLine(i.Type.ToString());
            Console.WriteLine(i.Town.ToString());
            Console.WriteLine(i.Time.ToString());
    }

    Well,
    this helped me outputting the list to know what is happening.

    So I re-edited the code to this condition:

    public static DateTime DailyWarTime = DateTime.Now;
    private static void InitTimeEntries()
            {
                var now = DateTime.Now.Date;
                var endDate = DateTime.Now.AddDays(30); // to check if the items add for 30 days, then end the while.
                
                // notice the difference in the following:
                var Town1WarTime = DailyWarTime.AddHours(3);
                var Town2WarTime = DailyWarTime.AddHours(6);
                var Town3WarTime = DailyWarTime.AddHours(7);
                var Town3WarTime2 = DailyWarTime.Date.AddHours(8);
    
                while (now < endDate)
                {
                    // 1 Item
                    TimeEntries.Add(new BattleGroundTimeEntry
                    {
                        Type = BattlegroundType.Deathmatch,
                        Town = BattlegroundTown.Town1,
                        Time = Town1WarTime
                    });
                    // 2 Item
                    TimeEntries.Add(new BattleGroundTimeEntry
                    {
                        Type = BattlegroundType.Deathmatch,
                        Town = BattlegroundTown.Town2,
                        Time = Town2WarTime
                    });
                    // 3 Item
                    TimeEntries.Add(new BattleGroundTimeEntry
                    {
                        Type = BattlegroundType.Occupation,
                        Town = BattlegroundTown.Town3,
                        Time = Town3WarTime,
                    });
                    // 4 Item
                    TimeEntries.Add(new BattleGroundTimeEntry
                    {
                        Type = BattlegroundType.Deathmatch,
                        Town = BattlegroundTown.Town3,
                        Time = Town3WarTime2,
                    });
    
                    now = now.AddMinutes(BeetweenWarsMinutes); //this need to be edited, without it, causes unlimited loop.
                    foreach (BattleGroundTimeEntry i in TimeEntries)
                    {
                        Console.WriteLine(i.Type.ToString());
                        Console.WriteLine(i.Town.ToString());
                        Console.WriteLine(i.Time.ToString());
                    }
                }
            }

    this outputs:

    the first 3 items date times are not correct, because

    public static DateTime DailyWarTime = DateTime.Now;
    var Town1WarTime = DailyWarTime.AddHours(3);
    // outputs: 21/02/2018 08:05:32 PM, incorrect datetime, contains current minutes & seconds in DateTime.Now.
    
    var Town3WarTime2 = DailyWarTime.Date.AddHours(8);
    // outputs: 21/02/2018 08:00:00 AM, correct but I'm worried about the PM/AM, and I want it only in PM.

    Is the timing a 24 hours clock or 12 ?
    I'm guessing its a 24 so it will be like this:

    var Town1WarTime = DailyWarTime.Date.AddHours(15);
    var Town2WarTime = DailyWarTime.Date.AddHours(18);
    var Town3WarTime = DailyWarTime.Date.AddHours(19);
    var Town3WarTime2 = DailyWarTime.Date.AddHours(20);

    outputs:

    this outputs the time for the same day constantly, how do I move it to the next day?
    like:

    Deathmatch
    Town1
    21/02/2018 03:00:00 PM
    Deathmatch
    Town2
    21/02/2018 06:00:00 PM
    Occupation
    Town3
    21/02/2018 07:00:00 PM
    Deathmatch
    Town3
    21/02/2018 08:00:00 PM
    Deathmatch
    // adding ended, re-add the same items for the the NextDay
    Town1
    22/02/2018 03:00:00 PM
    Deathmatch
    Town2
    22/02/2018 06:00:00 PM
    Occupation
    Town3
    22/02/2018 07:00:00 PM
    Deathmatch
    Town3
    22/02/2018 08:00:00 PM

    and please look at the line 44:

    now = now.AddMinutes(BeetweenWarsMinutes);
    since I edited the timing, how do I change this?


    Please help, and sorry for making this too long, just tried to explain this in each detail.

    • Edited by Zuher Laith Wednesday, February 21, 2018 10:34 AM
    Wednesday, February 21, 2018 10:33 AM
  • This should go outside your while loop:

                   foreach (BattleGroundTimeEntry i in TimeEntries)
                    {
                        Console.WriteLine(i.Type.ToString());
                        Console.WriteLine(i.Town.ToString());
                        Console.WriteLine(i.Time.ToString());
                    }

     


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, February 21, 2018 3:23 PM
    Moderator
  • This should go outside your while loop:

                   foreach (BattleGroundTimeEntry i in TimeEntries)
                    {
                        Console.WriteLine(i.Type.ToString());
                        Console.WriteLine(i.Town.ToString());
                        Console.WriteLine(i.Time.ToString());
                    }

    Still not a solution about the NextDay Date for the Items added ..
    Wednesday, February 21, 2018 3:46 PM
  • up .. anyone?
    Wednesday, February 21, 2018 9:20 PM
  • Could you post the code you're using now? You've changed it a bit here and there and it's hard to follow what your code actually is ...

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, February 21, 2018 9:27 PM
    Moderator
  • Could you post the code you're using now? You've changed it a bit here and there and it's hard to follow what your code actually is ...

    public class BattleGroundTimeEntry
            {
                public BattlegroundType Type { get; set; }
                public BattlegroundTown Town { get; set; }
                public DateTime Time { get; set; }
            }
    
            public enum BattlegroundType
            {
                Occupation = 0,
                Deathmatch = 1
            }
    
            public enum BattlegroundTown
            {
                Town1 = 0,
                Town2 = 1,
                Town3 = 3
            }
            public static DateTime DailyWarTime = DateTime.Now;
    
            private static void InitTimeEntries()
            {
                var now = DateTime.Now.Date;
                var endDate = DateTime.Now.AddDays(0);
    
                var Town1WarTime = DailyWarTime.Date.AddHours(15);
                var Town2WarTime = DailyWarTime.Date.AddHours(18);
                var Town3WarTime = DailyWarTime.Date.AddHours(19);
                var Town3WarTime2 = DailyWarTime.Date.AddHours(20);
    
                while (now < endDate)
                {
                    TimeEntries.Add(new BattleGroundTimeEntry
                    {
                        Type = BattlegroundType.Deathmatch,
                        Town = BattlegroundTown.Town1,
                        Time = Town1WarTime
                    });
                    TimeEntries.Add(new BattleGroundTimeEntry
                    {
                        Type = BattlegroundType.Deathmatch,
                        Town = BattlegroundTown.Town2,
                        Time = Town2WarTime
                    });
                    TimeEntries.Add(new BattleGroundTimeEntry
                    {
                        Type = BattlegroundType.Occupation,
                        Town = BattlegroundTown.Town3,
                        Time = Town3WarTime,
                    });
                    TimeEntries.Add(new BattleGroundTimeEntry
                    {
                        Type = BattlegroundType.Deathmatch,
                        Town = BattlegroundTown.Town3,
                        Time = Town3WarTime2,
                    });
    
    
                    now = now.AddMinutes(BeetweenWarsMinutes); //this need to be edited
                }
    
                foreach (BattleGroundTimeEntry i in TimeEntries)
                {
                    Console.WriteLine(i.Type.ToString());
                    Console.WriteLine(i.Town.ToString());
                    Console.WriteLine(i.Time.ToString());
                }
            }

    this is a bit corrupted, read InitTimeEntries.
    this function basically produces a specific items added with date time on a specific duration by days.
    so basically this is how the items should be for 0 day only (which means the current day):
    Deathmatch
    Town1
    22/02/2018 03:00:00 PM // Town1WarTime
    Deathmatch
    Town2
    22/02/2018 06:00:00 PM // Town2WarTime
    Occupation
    Town3
    22/02/2018 07:00:00 PM // Town3WarTime
    Deathmatch
    Town3
    22/02/2018 08:00:00 PM // Town3WarTime2
    and repeats adding the same items with only the day in Town1WarTime/Town2WarTime/... changed until it completes a specific value of days in "endDate" var.

    I hope you now understand, any questions please feel free to ask

    • Edited by Zuher Laith Wednesday, February 21, 2018 10:11 PM
    Wednesday, February 21, 2018 9:58 PM
  • Sorry for the delay in replying (I had an appointment and was away from my computer).

    So what results do you want to see? The way you have coded it now, it will keep using the same WarTimes for each of the Towns, until the loop ends (which will end when now > endDate, which will be what the current time was when the loop started . You probably expect those times to change, but change to what? My point is that you're never changing TownXWarTime and I'm guessing that you meant to do that, but I really don't know for sure what your intent is.  BTW, this will currently only do a loop for the one day, if that was not your intention, then you need to modify how you set endDate.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, February 22, 2018 12:38 AM
    Moderator
  • Sorry for the delay in replying (I had an appointment and was away from my computer).

    My point is that you're never changing TownXWarTime and I'm guessing that you meant to do that, but I really don't know for sure what your intent is. 

    BTW, this will currently only do a loop for the one day, if that was not your intention, then you need to modify how you set endDate.

    Its ok BonnieB

    Exactly,
    Yes I means to do that, cause I have no idea how to make it change when I use more than one day in endDate

    It's not my intention to make it for one day, what do you suggest on setting endDate ?

    and I apologize for the late reply.

    Thursday, February 22, 2018 7:30 PM
  • Hi Zuher,

    Let's go back to your original code. The only thing missing from that was how to do the Console.WriteLine() and where to put it. This should give you want you want:

    private readonly static List<BattleGroundTimeEntry> TimeEntries = new List<BattleGroundTimeEntry>();
    public static byte BeetweenWarsMinutes = 180;
    
    public enum BattlegroundType
    {
        Occupation,
        Deathmatch
    }
    public enum BattlegroundTown
    {
        Town1,
        Town2,
        Town3
    }
    
    public class BattleGroundTimeEntry
    {
        public BattlegroundType Type { get; set; }
        public BattlegroundTown Town { get; set; }
        public DateTime Time { get; set; }
    }
    
    private static void InitTimeEntries()
    {
        var now = DateTime.Now.Date;
        var endDate = DateTime.Now.AddDays(30);
        while (now < endDate)
        {
            foreach (BattlegroundType type in Enum.GetValues(typeof(BattlegroundType)))
            {
                foreach (BattlegroundTown town in Enum.GetValues(typeof(BattlegroundTown)))
                {
                    TimeEntries.Add(new BattleGroundTimeEntry
                    {
                        Type = type,
                        Town = town,
                        Time = now
                    });
                    now = now.AddMinutes(BeetweenWarsMinutes);
                }
            }
        }
        foreach (BattleGroundTimeEntry i in TimeEntries)
        {
            Console.WriteLine(i.Type.ToString());
            Console.WriteLine(i.Town.ToString());
            Console.WriteLine(i.Time.ToString());
        }
    }
    

    I only added the part in bold, everything else was the code you originally posted. And this works fine. If you want the time between Wars to vary (not be 180 minutes between each War), then I hope you can figure out what you need to change to make that happen. If not, let us know ... I haven't had time to think about that, but then again, I didn't know if you needed that change or not.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, February 22, 2018 9:06 PM
    Moderator

  • I only added the part in bold, everything else was the code you originally posted. And this works fine. If you want the time between Wars to vary (not be 180 minutes between each War), then I hope you can figure out what you need to change to make that happen. If not, let us know ... I haven't had time to think about that, but then again, I didn't know if you needed that change or not.

    both of the following outputs are for one day for testing purposes.

    this is the output for this code, works with more than a day (it uses BeetweenWarsMinutes):
    Occupation
    Town1 23/02/2018 12:00:00 AM Occupation Town2 23/02/2018 03:00:00 AM Occupation Town3 23/02/2018 06:00:00 AM Occupation Town3 23/02/2018 09:00:00 AM Deathmatch Town1 23/02/2018 12:00:00 PM Deathmatch Town2 23/02/2018 03:00:00 PM Deathmatch Town3 23/02/2018 06:00:00 PM Deathmatch Town3 23/02/2018 09:00:00 PM

    but I want the output to be simply like this (for more than a day & without BeetweenWarsMinutes (specific day timing)):
    Deathmatch
    Town1
    23/02/2018 03:00:00 PM
    Deathmatch
    Town2
    23/02/2018 06:00:00 PM
    Deathmatch
    Town3
    23/02/2018 07:00:00 PM
    Occupation
    Town3
    23/02/2018 08:00:00 PM

    I am terribly sorry for making this a long subject.

    • Edited by Zuher Laith Thursday, February 22, 2018 10:41 PM
    Thursday, February 22, 2018 10:38 PM
  • Hello,

     Just to clarify what your project goal is;  You have a daily

    schedule of events at predetermined times and want to

    listing generated on a daily basis?

     Is that correct? :)

    Friday, February 23, 2018 6:04 AM
  • OK Zuher,

    Try this:

    private readonly static List<BattleGroundTimeEntry> TimeEntries = new List<BattleGroundTimeEntry>();
    
    public enum BattlegroundType
    {
        Occupation,
        Deathmatch
    }
    public enum BattlegroundTown
    {
        Town1,
        Town2,
        Town3
    }
    
    public class BattleGroundTimeEntry
    {
        public BattlegroundType Type { get; set; }
        public BattlegroundTown Town { get; set; }
        public DateTime Time { get; set; }
    }
    
    private static void InitTimeEntries()
    {
        var now = DateTime.Now.Date;
        var endDate = DateTime.Now.AddDays(30);
        
        // changed functionality to this:
        DateTime WarTime;
        while (now < endDate)
        {
            WarTime = now.Date.AddHours(15);
            foreach (BattlegroundTown town in Enum.GetValues(typeof(BattlegroundTown)))
            {
                TimeEntries.Add(new BattleGroundTimeEntry
                {
                    Type = BattlegroundType.Deathmatch,
                    Town = town,
                    Time = WarTime
                });
                if (town == BattlegroundTown.Town1)
                    WarTime = WarTime.AddHours(3);
                else
                    WarTime = WarTime.AddHours(1);
            }
            TimeEntries.Add(new BattleGroundTimeEntry
            {
                Type = BattlegroundType.Occupation,
                Town = BattlegroundTown.Town3,
                Time = WarTime
            });
            now = now.AddDays(1);
        }
        
        foreach (BattleGroundTimeEntry i in TimeEntries)
        {
            Console.WriteLine(i.Type.ToString());
            Console.WriteLine(i.Town.ToString());
            Console.WriteLine(i.Time.ToString());
        }
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Zuher Laith Sunday, February 25, 2018 11:34 PM
    Friday, February 23, 2018 6:10 AM
    Moderator
  • Hi BonnieB

    I'm really sorry for the late reply, I wanted to make sure this works, it took sometime.
    Thank you so much for all the help you gave, really appreciate it!

    Best Regards.

    Sunday, February 25, 2018 11:34 PM
  • You're very welcome, Zuher!  I'm glad I could help!  =0)

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, February 26, 2018 12:28 AM
    Moderator