Home > Software design >  Writing data in a loop or otherwise
Writing data in a loop or otherwise

Time:01-09

I have a trivial question for you, perhaps. I am writing a Xamarin.Android application. A calendar to be exact. I have a String [] list of a given class in it. I have written data for each month and day like this:

 // January
 JavaList<Class_Event> fullList; fullList = new JavaList<Class_Event>();
 fullList.Add(new Class_Event(new DateTime(2022, 01, 1, 10, 0, 0), new DateTime(2022, 01, 1, 12, 0, 0), AppResources.ResourceManager.GetString("ev_01_01_2022"), Color.WhiteSmoke));

 ...

 fullList.Add(new Class_Event(new DateTime(2022, 01, 31, 10, 0, 0), new DateTime(2022, 01, 31, 12, 0, 0), AppResources.ResourceManager.GetString("ev_31_01_2022"), Color.WhiteSmoke));
 return fullList;

And this is what my class looks like:

class Class_Event
{
    DateTime startTime;
    DateTime endTime;
    string subject;
    Color robe_color;

    public Class_Event(DateTime StartTime, DateTime EndTime, string Subject, Color Robe_Color)
    {
        this.startTime = StartTime;
        this.endTime = EndTime;
        this.subject = Subject;
        this.robe_color = Robe_Color;
    }


    public DateTime StartTime
    {
        get { return startTime; }
    }
    public DateTime EndTime
    {
        get { return endTime; }
    }
    public string Subject
    {
        get { return subject; }
    }       
    public Color Robe_Color
    {
        get { return robe_color; }
    } 

Unfortunately, listing all 365 days of the year is quite tedious. What do you think. Could you save this data in a loop or use something else? For me, an attempt to write this data in a loop was in the place of adding an event color. I'm sorry for my writing. I use Google Translate :)

CodePudding user response:

you should be able to do something like this

DateTime first = new DateTime(2022, 01, 1, 10, 0, 0);

for (var ndx = 0; ndx < 365; ndx  )
{
   var start = first.AddDays(ndx);
   var end = start.AddHours(2);
   var desc = AppResources.ResourceManager.GetString($"ev_{start.Month}_{start.Day}_{start.Year}")

   var class = new Class_Event(start, end, desc, Color.WhiteSmoke);
   fullList.Add(class);
}
  •  Tags:  
  • Related