0

Is it possible to change the arrayname in a for loop? My Arrays are called T1, T2 T3... now I want to replace the number behind the T into an int. Here my code:

sql = con.CreateCommand();
sql.CommandText = "select [ZeitH] from [Tisch 1] where [Datum]='" + datum + "';";
OleDbDataReader reader = sql.ExecuteReader();
while (reader.Read())
{
    for (int i = 0; i <= 10; i++)
    {
        if (reader.GetInt32(0).ToString() == (i + 10).ToString())
        {
            T1[i] = false;
            T1[i + 1] = false;
            T1[i + 2] = false;
            T1[i + 3] = false;
        }
    }
}
6
  • I am not sure that you can change a collection's name while you are iterating Commented Nov 26, 2015 at 13:19
  • You want the T1 change to T2 and etc. ??? Commented Nov 26, 2015 at 13:20
  • 2
    I've reformatted your code to include the body of the if statement as normal vertical statements. Even if you'd normally put several statements on a line in your code (which I wouldn't recommend) it makes things particularly hard to read on Stack Overflow where there is limited horizontal space. Commented Nov 26, 2015 at 13:20
  • You can solve this problem using reflection but it's unlikely that it is the best possible solution. Just use jagged array instead T[][] and use for loop to iterate through your arrays. Commented Nov 26, 2015 at 13:21
  • why not create a function that takes a reference to T, and runs the for loop on the reference ? then it would be a matter of calling the function T1, T2, ..., TN times Commented Nov 26, 2015 at 13:25

2 Answers 2

4

If I understood you right - You can create an reference(name Temp),and assign to it T1.Then in the for loop you can iterate on that Temp reference, and if you want to change your array you can assign T2 to your Temp.

sql = con.CreateCommand();
sql.CommandText = "select [ZeitH] from [Tisch 1] where [Datum]='" + datum + "';";
OleDbDataReader reader = sql.ExecuteReader();

var Temp = T1;

while (reader.Read())
{
    for (int i = 0; i <= 10; i++)
    {
        if (reader.GetInt32(0).ToString() == (i + 10).ToString())
        {
            if( your condition)
            {
                Temp = T2;
            }

            Temp[i] = false;
            Temp[i + 1] = false;
            Temp[i + 2] = false;
            Temp[i + 3] = false;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If i understood you right, you want to replace the "1" after the T to some other number depending on some condition.

I would recommend you to store your arrays in a list or in another array (any ordered collection will do) and not keeping x Tx variables.

Something like:

List<bool[]> list = new List() { T1, T2, T3, T4, T5, T6 }; // add all your arrays, best generate the arrays in a loop and add them to the list in that loop

for (int i = 0; i <= 10; i++)
{
    if (reader.GetInt32(0).ToString() == (i + 10).ToString())
    {
        if(someCondition)
        {
            x = 1; // eg. take the T2 array if condition matches
        }
        else
        {
            x = 2; // eg. take the T3 array if condition does not match
        }
        list[x][i] = false;
        list[x][i + 1] = false;
        list[x][i + 2] = false;
        list[x][i + 3] = false;
    }
}

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.