3

Okay, so I have a <tr> table being built from a string that looks something like this: 13467

1= monday
2= tuesday
...
7= sunday
so 13467 = mon,wed,thurs,sat,sun

My existing code manually checked the string, like so

if (breakfastDays.Contains("1")) {
        sb.Append("<td class=\"active\">&nbsp;</td>");
        mo++;
    } else {
        sb.Append("<td>&nbsp;</td>");
    }

    if (breakfastDays.Contains("2")) {
        sb.Append("<td class=\"active\">&nbsp;</td>");
        tu++;
    } else {
        sb.Append("<td>&nbsp;</td>");
    }

The class="active"part just tells the css to check the box.

Is there an easier way of doing the string checking by any chance?

Perhaps some for-in loops maybe?

4
  • There's a lot here that's hard to understand, but if you're wondering if there's a better method to determine whether breakfastDays contains a "1" or a "5", breakfastDays.Contains() is about as good as it gets. Commented Dec 8, 2014 at 22:49
  • 1
    We might be in a better spot if you say why you have to increment mo and tu in this code. Commented Dec 8, 2014 at 22:50
  • 11
    Why don't you just eat breakfast every day? Problem solved! Commented Dec 8, 2014 at 22:51
  • Hmm. Building html with a string builder seems rather like a chore. You sure you don't wanna use a templating engine for that? weblogs.asp.net/scottgu/introducing-razor Commented Dec 8, 2014 at 23:08

4 Answers 4

7

Yes, you can use a loop. The only tricky thing is the variables that you increase, it would be easier if you used an array for that.

for (char c = '1'; c <= '7'; c++) {
  if (breakfastDays.Contains(c)) {
    sb.Append("<td class=\"active\">&nbsp;</td>");
    switch (c) {
      case '1': mo++; break;
      case '2': tu++; break;
      case '3': we++; break;
      case '4': th++; break;
      case '5': fr++; break;
      case '6': sa++; break;
      case '7': su++; break;
    }
  } else {
    sb.Append("<td>&nbsp;</td>");
  }
} 
Sign up to request clarification or add additional context in comments.

4 Comments

A perfect instance for the usage of a switch over if else if else. +1
I had to do Contains(Convert.ToString(c))) but other than that perfect answer. Thanks a bunch!
@DPB: Aha, the method that takes a char is an extension method in the System.Linq namespace. If you include that you can use the char directly. I tested the code in a project that already had it included.
@Guffa I just implemented System.Linq, thanks, works like a charm.
1

I think that mo and tu are counts that keep count of the days of the week. A better way might be an array:

public static void Main()
{
    int[] breakfastDays = new int[7];
    string days = "13467";
    var sb = new StringBuilder();

    for (int i = 0; i < 7; i++)
    {
        if (days.Contains((i + 1).ToString()))
        {
            sb.Append("<td class=\"active\">&nbsp;</td>");
            breakfastDays[i]++;
        } else {
            sb.Append("<td>&nbsp;</td>");
        }
    }

    Console.WriteLine(sb.ToString());
}

This way, you won't need separate variables for every day.

Comments

1

I would first see if you can get rid of individual variables for the day counters, that seems clunky and issue-prone. That, and reducing this to a simple loop, something like:

// Set up buckets for each day, with an initial count of 0
var dayMap = "1234567".ToDictionary(c => c, c => 0);

// For each key ("day")...
foreach (var day in dayMap.Keys.ToList())
{
    // Start writing the cell
    sb.Append("<td");
    // Check if input string contains that key
    if (breakfastDays.Contains(day)) 
    {
        // Increment the value in our day bucket
        dayMap[day]++;
        // Make the cell "active"
        sb.Append(" class=\"active\"");
    } 
    // Finish writing the cell
    sb.Append(">&nbsp;</td>");
}

Comments

0

This will fix the issue of having to write seven tests for the table:

for (int i = 1; i <= 7; i++)
{
    if (breakfastDays.Contains(i.ToString())
    {
        sb.Append("<td class=\"active\">&nbsp;</td>"));
    }
    else
    {
        sb.Append("<td>&nbsp;</td>");
    }
}

We could sure get more clever but I don't want to skip over the importance of mo++ and tu++ in your code.

Comments

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.