如何优化这个c ++代码 [英] how to optmize this c++ code
问题描述
我无法使用c ++在循环中动态更改变量名称,对此进行编码的最佳方法是什么?
< pre lang =cs> 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 ;
}
在这样的情况下,我建议使用循环。
您将拥有一系列控件并循环显示它们。
for ( int i = 0 ,count = StrToInt(EditParcela- > Text); i!= count; ++ i)
{
// edit []是在此代码
// 之前填充的数组,可能只有一次表单已创建。
edit [i] - > ParentColor = false ;
edit [i] - > ReadOnly = false ;
}
你的代码中可疑的是你没有做任何剩余的编辑...我想他们认为应该设置为true
。
因此在上面的代码中,循环可能是这样的:
for ( int i = 0 ,used = StrToInt(EditParcela-> Text),count = 7 / * 数组大小* / ; i!= count; ++ i)
{
// edit []是在此代码之前填充的数组
< span class =code-comment> // 并且可能只在创建表单时使用一次。
bool editInUse = i<用过的;
edit [i] - > ParentColor =!editInUse;
edit [i] - > ReadOnly =!editInUse;
}
我会这样做
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 = 假跨度>;
Form4-> Edit6-> ReadOnly = false ;
case 5 :
/ / if(在此处插入nullptr)
Form4-> Edit5-> ParentColor = 假跨度>;
Form4-> Edit5-> ReadOnly = false ;
case 4 :
/ / if(在此处插入nullptr)
Form4-> Edit4-> ParentColor = 假跨度>;
Form4-> Edit4-> ReadOnly = false ;
case 3 :
/ / if(在此处插入nullptr)
Form4-> Edit3-> ReadOnly = 假跨度>;
Form4-> Edit3-> ParentColor = false ;
case 2 :
/ / if(在此处插入nullptr)
Form4-> Edit2-> ParentColor = 假跨度>;
Form4-> Edit2-> ReadOnly = false ;
case 1 :
/ / if(insert nullptr check here)
Form4-> Edit1-> ParentColor = 假跨度>;
Form4-> Edit1-> ReadOnly = false ;
默认:
break ;
}
}
请注意,在每个交换机的情况下,'break;'语句都被忽略了这使我们能够贯穿所有其他案例。如果通过指针运算符
i cant change the variable name dynamically in a loop using c++, what is the best way to code this?
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;
}
In a situation like this one, I would recommand using a loop.
You would have an array of controls and loop through them.
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 totrue
.
Thus in above code, the loop woul be something like:
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; }
I'd do it like that
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
这篇关于如何优化这个c ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!