Reputation: 967
Is there a more efficient way to do the following? Basically I'd like to reference the tMemo name mcpx without referring directly to the object name like I am below:
if x = 1 then mcp1.Lines.Append(inttostr(cp[x]));
if x = 2 then mcp2.Lines.Append(inttostr(cp[x]));
if x = 3 then mcp3.Lines.Append(inttostr(cp[x]));
if x = 4 then mcp4.Lines.Append(inttostr(cp[x]));
if x = 5 then mcp5.Lines.Append(inttostr(cp[x]));
Upvotes: 5
Views: 1873
Reputation: 7299
In a modern Delphi putting the memos in an array can be done fairly easily. Then as mentioned by Sertac Akyuz, access them using an array index and skip the if.
Var
MemoArray : array of TMemo;
...
MemoArray := [mcp1,mcp2,mcp3,mcp4,mcp5];
...
// Zero based dynamic array, so need to do the - 1, no need for the if
MemoArray[x-1].Lines.Append(IntToStr(cp[x]));
Upvotes: 8