And yes I have all this variables lol. I knew that wasn’t the best aproach… |
Whenever you need to store
n items of
something, where
n may even vary, do
not use
n separate variables; instead use an array!
...or maybe an STL container like
std::vector
.
Note: The size
n of an array can be chosen freely as needed, but is still
fixed once the array was created.
An
std::vector
can grow/shrink dynamically, by adding or removing items.
In your last example how do I write it so that the backcolor turns green depending on the song ids. Without using all those ifs. |
Exactly as was shown in the example?
Just use a
loop to iterate all checkboxes, from
#0 to
#n-1, and update the color of each one, based on whatever condition you need.
1 2 3 4
|
for (size_t i = 0; i < NUM_CHECKBOXES; ++i)
{
checkBoxes[i]->BackColor = (some_condition) ? Color::Green : Color::White;
}
|
...is a shorthand for:
1 2 3 4 5
|
for (size_t i = 0; i < NUM_CHECKBOXES; ++i)
{
if (some_condition) checkBoxes[i]->BackColor = Color::Green;
else checkBoxes[i]->BackColor = Color::White;
}
|
In the above code, replace "some_condition" with the actual condition you need! (e.g.
checkBoxSongID[i] == 1
)
BTW: Variables like
checkBox163B_Song_ID
should also be converted into a single array!