Click here to Skip to main content
15,881,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I managed to change my code up and now it is running better, my only issue is that i cant use day/week twice.
I want it to output looking like this: http://i.stack.imgur.com/LZu7u.jpg[^]

Here is my code:


C#
int[,] toys = new int[5, 4];
            for (int week = 0; week <= 3; week++)
            {
                for (int day = 0; day <= 4; day++)
                {
                    int tempVal = 0;
                    if (int.TryParse(Microsoft.VisualBasic.Interaction.InputBox("Please enter value for Day " + (day + 1) + " in week " + (week + 1) + "."), out tempVal))
                    {
                        toys[day, week] = tempVal;
                    }
                    else
                    {
                        MessageBox.Show("Invalid entry");
                        day--;
                        continue;
                    }

txtOutput.Text += "\t\t" + "Mon" + "\t" + "Tue" + "\t" + "Wed" + "\t" + "Thu" + "\t" + "Fri" + "\t" + "\r\n";
     //here it says "A local variable named 'week' cannot be declared in this scope because it would give a different meaning to 'week', which is already used in a 'parent or current' scope to denote something else" For day & week
              
for (int week = 0; week <= 3; week++)//foreach week
                   {
                       //construct the line of text which represents the week's data
                       txtOutput.Text += "\tWeek " + (week + 1) + "\t";
                       for (int day = 0; day <= 4; day++)
                       {
                           txtOutput.Text += toys[day, week];
                           if (day != 4)
                           {
                               //so long as it is not the last day, then it tabs over
                               txtOutput.Text += "\t";
                           }
                       }
                       //moving over to the next line
                       txtOutput.Text += "\r\n\r\n";
                   }
Posted

1 solution

Take a look at string.Format...

Here's an easy version of it:

C#
string headerFormat = "\t\tMon\tTue\tWed\tThur\tFri\n";
string lineFormat = "Week {0}\t{1}\t{2}\t{3}\t{4}\t{5}\n";

textbox1.Text = headerFormat;
for (int week = 0; week < 4; week++)
{
    textbox1.AppendText(string.Format(lineFormat,
        week, toys[week, 0], toys[week, 1], toys[week, 2] toys[week, 3]
        toys[week, 4]));
}


Thats the easiest way to do it, you can play around with the format strings for each field to make them line up if they don't.
 
Share this answer
 
v2
Comments
Member 10540766 23-Jan-14 21:15pm    
Yeah checking it out now, but this gives me the same weird message "A local variable named 'week' cannot be declared in this scope because it would give a different meaning to 'week', which is already used in a 'parent or current' scope to denote something else" in the for int week part
Ron Beyer 23-Jan-14 21:17pm    
Just remove the "int" part in front of week in the for loop.

Member 10540766 23-Jan-14 21:33pm    
Didn't think it was that obvious. :)

Btw, with week 0,1,2,3,4. Does that output it as Week 0, Week 1 ... etc?
Because techincally i have Week 1-4 only in the array
Ron Beyer 23-Jan-14 21:36pm    
Just change week to week+1 inside the for loop and that will fix it.

Like : ...string..Format(lineFormat,
week + 1, toys[week, 0] ...
Member 10540766 23-Jan-14 21:47pm    
That seems to fix that issue, now i tried mixing up the for loop to change things up.

If i run ur code it gives me "Index was outside the bounds of the array."
If i run this it gives me "Index (zero based) must be greater than or equal to zero and less than the size of the argument list."
for (week = 0; week < 3; week++)
{
txtOutput.AppendText(string.Format(lineFormat, week + 1, toys[week, 0], toys[week, 1], toys[week, 2], toys[week, 3]));

Im trying to make it all make sense

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900