Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi Array with Objects

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]));
like image 806
user2021539 Avatar asked Jul 12 '19 17:07

user2021539


1 Answers

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]));
like image 94
Brian Avatar answered Sep 18 '22 16:09

Brian