1

Ok, I've been asked to do this simple program where depending on the day of the week a variable is outputted to a text box. Now the example is a timetable. I have done a variables class just so I get used to it.

class Variables
{
    public static string Monday = string.Format("Monday's Timetable{0}P1 English{0}P2 Maths{0}P3 History{0}P4 Computing", Environment.NewLine);
    public static string Tuesday = string.Format("Tuesday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine);
    public static string Wednesday = string.Format("Wednesday's Timetable{0}P1 Science{0}P2 English{0}P3 Computing{0}P4 I.T", Environment.NewLine);
    public static string Thursday = string.Format("Thurday's Timetable{0}P1 Geography{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine);
    public static string Friday = string.Format("Friday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine);
}

All that is stored and I've got this in the code for the form

private void tblcustom_Click(object sender, EventArgs e)
    {
        string customdayofweek = customdatepicker.Value.DayOfWeek.ToString();


    }

    private void tbltoday_Click(object sender, EventArgs e)
    {
        string dayofweektoday = DateTime.Today.DayOfWeek.ToString();

        outputtbl.Text = Variables. "Whatever the day of the week is"

    }

The question is so I don't have to do a load of if statements, is there a way that I can do it so whatever the day of the week is selected or it is today, it will display the variable.

I hope it's clear, I can't seem to find it on the internet as well I don't really know what I'm looking for that's why I'm here

Thanks in advance

Kieran

Joe
  • 7,113
  • 1
  • 29
  • 34
kieran2203
  • 19
  • 3
  • @MitchWheat It's so I dont need to do if( dayofweek == monday) output.text = varables.monday it will do it whatever is contained in dayofweektoday if that makes sense – kieran2203 Jan 20 '14 at 15:02
  • 2
    So reworded: is there an alternative way to pull the relevant day of week details out without 7 if-else statements, as in, key it from a dictionary or something. – Adam Houldsworth Jan 20 '14 at 15:02
  • http://msdn.microsoft.com/en-us/library/06tc147t.aspx – Claies Jan 20 '14 at 15:03
  • seems like you need a proper data model for this. A class that contains an instance of `DayOfWeek` as a property, plus a List or something that you can use to build up the time tables. and the like – Federico Berasategui Jan 20 '14 at 15:04
  • The four answers below are all excellent and different approaches to resolving your question. All of the answers below do not include Saturday or Sunday (as your example did not), so you should add logic to handle days not covered by the solution you use. – AWinkle Jan 20 '14 at 15:20

4 Answers4

4

The best way to do this without changing too much of your code is changing your static strings into a Dictionary<string, string>. Then, you can do something like this :

outputtbl.Text = Variables.YourDictionary[dayofweektoday];
krimog
  • 1,257
  • 11
  • 25
  • What do you mean by changing it to a dictionary, just c# is very new to me :) – kieran2203 Jan 20 '14 at 15:06
  • 1
    @user3215561 So look up what a Dictionary in C# is then ... http://www.dotnetperls.com/dictionary – Mashton Jan 20 '14 at 15:11
  • 1
    Create a field like this: `public static Dictionary Timetables;` Then, in the static constructor, you can instanciate and fill this dictionary: 'Timetables = new Dictionary(); Timetables.Add("Monday", "Monday's timetable{0}...");` – krimog Jan 20 '14 at 15:17
  • Or even better, use a `Dictionary` – krimog Jan 20 '14 at 15:22
2

Instead of having multiple variables use array like following:

string[] timetable = {
          string.Format("Monday's Timetable{0}P1 English{0}P2 Maths{0}P3 History{0}P4  Computing", Environment.NewLine), 
          string.Format("Tuesday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine),
          ...};

Then you can use timetable[(int)DateTime.Today.DayOfWeek] to selected timetable.

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
  • How would I then pull mondays timetable out of it if the calender says monday – kieran2203 Jan 20 '14 at 15:10
  • I updated my answer. When you cast DayOfWeek to integer it will give you correct number. You can read more here: http://stackoverflow.com/questions/9199080/how-to-get-the-integer-value-of-day-of-week – Piotr Stapp Jan 20 '14 at 15:15
1

Use a dictionary to store the relationship between DayOfWeek and your string.

class Variables
{
    Dictionary<DayOfWeek, string> DayText = new Dictionary<DayOfWeek, string>()
    {
       {DayOfWeek.Monday, string.Format("Monday's Timetable{0}P1 English{0}P2 Maths{0}P3 History{0}P4 Computing", Environment.NewLine)},
       {DayOfWeek.Tuesday, string.Format("Tuesday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine)},
       {DayOfWeek.Wednesday, string.Format("Wednesday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine)},
       {DayOfWeek.Thursday, string.Format("Thursday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine)},
       {DayOfWeek.Friday , string.Format("Friday 's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine)},
    };    

}

Then use the enum value of the current day as a key to retrieve the value from the dictionary

private void tbltoday_Click(object sender, EventArgs e)
{
    DayOfWeek v = DateTime.Today.DayOfWeek;
    if(v != DayOfWeek.Sunday && v != DayOfWeek.Saturday)
        outputtbl.Text = Variables.DayText[v];

}
Steve
  • 213,761
  • 22
  • 232
  • 286
0

Borrowing the concept from c-sharp-get-values-of-static-properties-from-static-class I wrote a sample block of code in C# Winforms. This example will pull the current Day of Week's static string property from Variables. The only using statement you need is using System;

    private void RetrieveDayOfWeekText()
    {
        Variables myInstance = new Variables();
        this.richTextBox1.Text = myInstance.GetType().GetField(DateTime.Now.ToString("dddd")).GetValue(myInstance).ToString();
    }
Community
  • 1
  • 1
AWinkle
  • 673
  • 8
  • 18
  • Although this would work, please, please don't use it. The approach with a dictionary / array is much cleaner. – Joe Jan 20 '14 at 15:18
  • I'm not sure I understand the hesitance @JoeStead; learning to use reflection is a good thing. – AWinkle Jan 20 '14 at 15:22
  • 2
    agreed that learning how to use reflection is a good thing, given the poster is still getting to grips with the basics, do you not think this is a little bit irrelevant? Reflection should be avoided if at all possible, and in this scenario it can be. – Joe Jan 20 '14 at 15:24
  • Developer blinders failed me. I can't agree with you more. – AWinkle Jan 20 '14 at 15:40