Carl Dun
Carl Dun

Reputation: 235

What is the correct syntax for my for loop in C#?

I've made a wpf program and have this for loop to get the name of the item, qty and prc's values and convert them to string to display them on the spreadsheet that I loaded within the application.

So far my working code is this:

worksheet1.Range["C14:D14:E14"].Value = got_item;
worksheet1.Cells["A14"].Value = qty.Value.ToString();
worksheet1.Cells["G14"].Value = prc.Value.ToString();
v1 = got_value;
i1q = Int32.Parse(qty.Value.ToString());
i1p = prc.Value;

However, I will have to repeat those for 12 more times until:

worksheet1.Range["C26:D26:E26"].Value = got_item;
worksheet1.Cells["A26"].Value = qty.Value.ToString();
worksheet1.Cells["G26"].Value = prc.Value.ToString();
v13 = got_value;
i13q = Int32.Parse(qty.Value.ToString());
i13p = prc.Value;

It's working good but it isn't very appealing to look at so I tried to create a for loop for it but I have little to no experience with for loops.

I tried my best and so far here is my progress:

for(x=0;x<13;x++)
{
    y += x + 13;
    worksheet1.Range["C"+y+":D"+y+":E"+y].Value = "";
    worksheet1.Cells["A"+y].Value = qty.Value.ToString();
    worksheet1.Cells["G"+y].Value = prc.Value.ToString();
    v + x = got_value;
    i + x + q = Int32.Parse(qty.Value.ToString());
    i + x + p = prc.Value;
}

I'm having troubles connecting the for loop values of x and y:

  1. y to the worksheet range and cells
  2. x to call the v(x), i(x)q and i(x)p

UPDATE: Thanks to Jcl, here is my final result:

for (x = 0; x < 13; x++)
{
    y = x + 14;
    y.ToString();
    b[x] = worksheet1.Range["C" + y + ":D" + y + ":E" + y].Value.ToString();

    if (b[x] == "")
        {
            worksheet1.Range["C" + y + ":D" + y + ":E" + y].Value = got_item;
            worksheet1.Cells["A" + y].Value = qty.Value.ToString();
            worksheet1.Cells["G" + y].Value = prc.Value.ToString();
            v[x] = got_value;
            iq[x] = Int32.Parse(qty.Value.ToString());
            ip[x] = prc.Value;
        }
}

Although this doesn't work off the bat since the if statement is getting looped without an else if, a simple solution would be to just create an individual if statement and loop the else if part for 12 times.

I'll try studying cases if it could be applied but nevertheless, thank you again Jcl. Just knocked off multiple lines of code and learned looping at the same time.

Upvotes: 2

Views: 549

Answers (1)

Jcl
Jcl

Reputation: 28272

The first part of the problem is that here:

y += x + 13;

You are incrementing the value of y by x+13.

So in the first iteration, y is 0, and x is 0, you get y=13. In the second iteration though, y is 13 and x is 1. So you increment y by 1+13, and end up with y=27. In the third iteration, y is 27 and x is 2, so you increment 27+(13+2), so y=42. (etc.)

If in your first iteration, your y must be 13, in the second iteration, must be 14, etc., then you need to put it like:

y = x + 13;

Without the +=

The second part of the problem is that you can't just evaluate the result of an expression to a name of a variable (it can be done via reflection, but let's forget that).

So instead of having variables v1, v2, v3, etc, and i1q, i2q, etc., what make sense here is having some kind of array or list.

So wherever you are defining those variables, you should have something like (I can't figure out the types because the names you gave are not very descriptive, so I'll figure they are integers, change the type as appropiate):

int[] v = new int[14];
int[] iq = new int[14];
int[] ip = new int[14];

So when accessing those variables in the loop:

v[x] = got_value;
iq[x] = Int32.Parse(qty.Value.ToString());
ip[x] = prc.Value;

Note that arrays in c# are based on the index 0, but that's right, since your for loop, as currently stated, iterates from 0 to 12 (not from 1 to 13).

Upvotes: 3

Related Questions