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

5
  • @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 Commented Jan 20, 2014 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. Commented Jan 20, 2014 at 15:02
  • msdn.microsoft.com/en-us/library/06tc147t.aspx Commented Jan 20, 2014 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<string> or something that you can use to build up the time tables. and the like Commented Jan 20, 2014 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. Commented Jan 20, 2014 at 15:20

4 Answers 4

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];
Sign up to request clarification or add additional context in comments.

4 Comments

What do you mean by changing it to a dictionary, just c# is very new to me :)
@user3215561 So look up what a Dictionary in C# is then ... dotnetperls.com/dictionary
Create a field like this: public static Dictionary<string, string> Timetables; Then, in the static constructor, you can instanciate and fill this dictionary: 'Timetables = new Dictionary<string, string>(); Timetables.Add("Monday", "Monday's timetable{0}...");`
Or even better, use a Dictionary<DayOfWeek, string>
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.

2 Comments

How would I then pull mondays timetable out of it if the calender says monday
I updated my answer. When you cast DayOfWeek to integer it will give you correct number. You can read more here: stackoverflow.com/questions/9199080/…
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];

}

Comments

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

4 Comments

Although this would work, please, please don't use it. The approach with a dictionary / array is much cleaner.
I'm not sure I understand the hesitance @JoeStead; learning to use reflection is a good thing.
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.
Developer blinders failed me. I can't agree with you more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.