I'm trying creating a TAction
in runtime and insert in TActionClientItem
, but it's give me an error at runtime (Invalid class typecast).
I'm using this way:
function TFunctions.AddMenuItem(aciParent: TActionClientItem): integer;
var
ClientItem: TActionClientItem;
ActionToAdd: TAction;
begin
ClientItem:= aciParent.items.add;
ClientItem.Action := ActionToAdd; // <- error
//
end;
CodePudding user response:
Should mark the code block above as code so that it gets formatted properly (or at least as a preformatted block). Where is the "begin" statement? After the var block with the local variables declarations (you have two of those, ClientItem and ActionToAdd) there should be a "begin" statement and then your imperative commands, then have "end" statement to close the function declaration.
The error is from the parsing stage if "begin" is missing, but I'd expect it to be at ClientItem:= aciParent.items.add; (at the := point)
Obviously before "end" should also assign the "result" variable or can also return a result passing it as parameter to the "exit" statement if you need to have multiple exit points from your function. Use procedure instead of function if no result needed
CodePudding user response:
You mentioned in a comment that your code lives in a DLL. DLLs have their own copy of the VCL distinct from the one in the executable and mixing objects from different VCL instances doesn't work. You should switch to a package instead. These are built to handle these issues.