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]));
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]));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With