Click here to Skip to main content
15,896,528 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
i cant change the variable name dynamically in a loop using c++, what is the best way to code this?


C#
if (StrToInt(EditParcela->Text) == 2){
        Form4->Edit1->ParentColor = false;
        Form4->Edit1->ReadOnly = false;
        Form4->Edit2->ParentColor = false;
        Form4->Edit2->ReadOnly = false;
    }
    else if (StrToInt(EditParcela->Text) == 3){
        Form4->Edit1->ParentColor = false;
        Form4->Edit1->ReadOnly = false;
        Form4->Edit2->ParentColor = false;
        Form4->Edit2->ReadOnly = false;
        Form4->Edit3->ParentColor = false;
        Form4->Edit3->ReadOnly = false;
    }
    else if (StrToInt(EditParcela->Text) == 4){
        Form4->Edit1->ParentColor = false;
        Form4->Edit1->ReadOnly = false;
        Form4->Edit2->ParentColor = false;
        Form4->Edit2->ReadOnly = false;
        Form4->Edit3->ParentColor = false;
        Form4->Edit3->ReadOnly = false;
        Form4->Edit4->ParentColor = false;
        Form4->Edit4->ReadOnly = false;
    }
    else if (StrToInt(EditParcela->Text) == 5){
        Form4->Edit1->ParentColor = false;
        Form4->Edit1->ReadOnly = false;
        Form4->Edit2->ParentColor = false;
        Form4->Edit2->ReadOnly = false;
        Form4->Edit3->ParentColor = false;
        Form4->Edit3->ReadOnly = false;
        Form4->Edit4->ParentColor = false;
        Form4->Edit4->ReadOnly = false;
        Form4->Edit5->ParentColor = false;
        Form4->Edit5->ReadOnly = false;
    }
    else if (StrToInt(EditParcela->Text) == 6){
        Form4->Edit1->ParentColor = false;
        Form4->Edit1->ReadOnly = false;
        Form4->Edit2->ParentColor = false;
        Form4->Edit2->ReadOnly = false;
        Form4->Edit3->ParentColor = false;
        Form4->Edit3->ReadOnly = false;
        Form4->Edit4->ParentColor = false;
        Form4->Edit4->ReadOnly = false;
        Form4->Edit5->ParentColor = false;
        Form4->Edit5->ReadOnly = false;
        Form4->Edit6->ParentColor = false;
        Form4->Edit6->ReadOnly = false;
    }
    else if (StrToInt(EditParcela->Text) == 7){
        Form4->Edit1->ParentColor = false;
        Form4->Edit1->ReadOnly = false;
        Form4->Edit2->ParentColor = false;
        Form4->Edit2->ReadOnly = false;
        Form4->Edit3->ParentColor = false;
        Form4->Edit3->ReadOnly = false;
        Form4->Edit4->ParentColor = false;
        Form4->Edit4->ReadOnly = false;
        Form4->Edit5->ParentColor = false;
        Form4->Edit5->ReadOnly = false;
        Form4->Edit6->ParentColor = false;
        Form4->Edit6->ReadOnly = false;
        Form4->Edit7->ParentColor = false;
        Form4->Edit7->ReadOnly = false;
    }
Posted
Comments
Richard MacCutchan 30-Dec-13 6:48am    
Convert the string once only, set all common values, and use a switch statement for the rest. Remember also that you can fall through from one case to the next where appropriate.
uSergim 30-Dec-13 6:54am    
Thank you guys , that is much better!
Hamed Moezzi Azimi 30-Dec-13 14:52pm    
hi
I think you can use the pointer to the function.

In a situation like this one, I would recommand using a loop.

You would have an array of controls and loop through them.

C++
for (int i = 0, count = StrToInt(EditParcela->Text); i != count; ++i)
{
  // edit[] is an array that was filled before this code 
  // and possibly only once when the form is created.

  edit[i]->ParentColor = false;
  edit[i]->ReadOnly = false;
}


What is suspicious in your code is that you do nothing for remainding edits... I would thinkg that they should be set to true.

Thus in above code, the loop woul be something like:

C++
for (int i = 0, used = StrToInt(EditParcela->Text), count = 7 /* size of the array */; i != count; ++i)
{
  // edit[] is an array that was filled before this code 
  // and possibly only once when the form is created.

  bool editInUse = i < used;
  edit[i]->ParentColor = !editInUse;
  edit[i]->ReadOnly = !editInUse;
}
 
Share this answer
 
I'd do it like that

C#
if(EditParcela->Text != nullptr)
{
    switch(StrToInt(EditParcela->Text))
    {
        case 7:
            // if( insert nullptr check here)
            Form4->Edit7->ParentColor = false;
            Form4->Edit7->ReadOnly = false;
        case 6:
            // if( insert nullptr check here)
            Form4->Edit6->ParentColor = false;
            Form4->Edit6->ReadOnly = false;
        case 5:
            // if( insert nullptr check here)
            Form4->Edit5->ParentColor = false;
            Form4->Edit5->ReadOnly = false;
        case 4:
            // if( insert nullptr check here)
            Form4->Edit4->ParentColor = false;
            Form4->Edit4->ReadOnly = false;
        case 3:
            // if( insert nullptr check here)
            Form4->Edit3->ReadOnly = false;
            Form4->Edit3->ParentColor = false;
        case 2:
            // if( insert nullptr check here)
            Form4->Edit2->ParentColor = false;
            Form4->Edit2->ReadOnly = false;
        case 1:
            // if( insert nullptr check here)
            Form4->Edit1->ParentColor = false;
            Form4->Edit1->ReadOnly = false;
        default:
            break;
    }
}


Note that in each case of the switch the 'break;' statement is left out which enables us to run through all other cases as well. Also check for nullpointer if you access members through the pointer operator
 
Share this answer
 
v2
Comments
uSergim 30-Dec-13 6:54am    
Thank you guys , that is much better!
Philippe Mori 30-Dec-13 8:44am    
Usually, one would add a // fall-through comment at the end of each case so that it is clear that it was intentional.

By the way for a case like that, a would rather recommand a loop (see my solution).
max_nowak 31-Dec-13 9:15am    
Wow, this fall-through comment is a great idea! Haven't come across this anywhere yet but I think I might introduce it to my coworkers as new convention. Good advice thanks

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