use*_*539 5 arrays delphi reference object
有没有更有效的方法来执行以下操作?基本上,我想引用tMemo名称mcpx而不直接引用对象名称,如下所示:
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]));
Run Code Online (Sandbox Code Playgroud)
在现代的Delphi中,将备忘录排列成数组很容易。然后,如Sertac Akyuz所述,使用数组索引访问它们,并跳过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]));
Run Code Online (Sandbox Code Playgroud)