Delphi Rio 10.3.3中如何内联静态数组变量?
Hey there, I've run into exactly this issue before! The E2029 error you're seeing is a syntax limitation specific to Delphi 10.3.3—the inline variable declaration feature (where you declare variables inside begin...end using var) doesn't support direct static array declarations like array[0..2047] of Char, even though static arrays work perfectly fine in the traditional top-level var block.
Here are three solid solutions to get around this:
1. Wrap the Static Array in a Type Alias
The easiest workaround is to define a type alias for your static array in the unit's type section, then use that alias for your inline variable:
type TFileNameBuffer = array[0..2047] of Char; procedure TForm1.AcceptFiles(var Msg: TWMDropFiles); begin var FileName: TFileNameBuffer; // No more E2029 error! DragQueryFile(msg.Drop, $FFFFFFFF, FileName, 2048); // Rest of your code here... end;
This keeps all the benefits of a static array (fixed size, stack allocation, no overhead) while playing nicely with 10.3.3's inline variable rules.
2. Upgrade to a Newer Delphi Version
Embarcadero fixed this limitation starting with Delphi 10.4 Sydney. If you can upgrade to 10.4 or later (like 11 Alexandria or 12 Athens), your original code will compile without any changes—inline static array declarations are fully supported in those versions.
3. Use a Dynamic Array (Last Resort)
If you can't define a type alias or upgrade, you can use a dynamic array as a temporary fix. Just make sure to set its length before using it with DragQueryFile:
procedure TForm1.AcceptFiles(var Msg: TWMDropFiles); begin var FileName: array of Char; SetLength(FileName, 2048); // Match the original static array size DragQueryFile(msg.Drop, $FFFFFFFF, FileName[0], 2048); // Rest of your code here... end;
Note that this adds dynamic memory allocation overhead, which isn't a big deal for file path handling, but static arrays are still better for performance and memory predictability. So only use this if the first two options aren't feasible.
To recap: The issue is just a version-specific syntax gap—your original code is logically correct, it just doesn't fit 10.3.3's inline variable rules. The type alias trick is the most straightforward fix for staying on 10.3.3.
内容的提问来源于stack exchange,提问作者Gad D Lord

