Quote:
Originally Posted by groofshark
That's for all menu. I want instead adding 20 if( g_options[id][iItem] ) just to make a loop and that loop to give on every "IF" - option and his number(iItem)
|
You don't need to loop unless you have a more complex situation.
Code:
/**
* Given this enum the following things are true:
* opt1 == 0 && opt2 == 1 && opt3 == 2
*/
enum _:options
{
opt1,
opt2,
opt3,
}
/**
* That means that this if can easily be reduced.
* Observe that when |iItem == 0| then |opt1 == iItem| is also true.
*/
if(g_options[id][opt1] && iItem == 0 )
{
return ITEM_DISABLED;
}
/**
* So instead we can write it as following, note that |opt1| has
* been replaced, and instead |iItem| is retrieved from the array.
* This does not change code behavior.
*/
if(g_options[id][iItem] && iItem == 0 )
{
return ITEM_DISABLED;
}
/**
* Since the code inside of your if statements does not
* differ (it always returns the same value), the |iItem == 0|
* check is actually redundant. This means that we can remove it
* and get rid of all the other if statements at the same time:
*/
if(g_options[id][iItem])
{
return ITEM_DISABLED;
}
A loop would unnecessarily complicate things. You need to do something based on a single value, and you have the means to directly derive the necessary information from that single value. You don't need to search for anything, you don't need to iterate. A lookup-table is perfect for this, and you already have it in place. You just need to use it properly.
__________________