ADrive, it may be possible that you have not fully understood just how Delphi typecasts work. First thing first - though not wrong, it does strike me as odd that you should assign the object you want to typecast as a TObject - I would have used a raw pointer. Secondly, what you have done is not really a typecast at all. I provide a few examples below which might point you in the right direction.
- Imagine that we have a TMaskEdit component on a form
- You want to use the OnClick event handler for this component
[]]The empty template Delphi gives you will go like this
procedure TMyForm.MaskEdit1Click(Sender:TObject)
begin
with TMaskEdit(Sender) do
begin
MaxLength:=32;
Text:='Top Secret';
end;
end;
Here we done the typecast as
TMaskEdit(Sender). The
with block ensures that the properties
MaxLength and
Text of your
TMaskEdit control can be correctly accessed.
Here is another way to do the same thing
procedure TMyForm.MaskEdit1Click(Sender:TObject)
var AEdit:TMaskEdit absolute Sender;
begin
with AEdit do
begin
MaxLength:=32;
Text:='Top Secret';
end;
end;
Here we dispense with the explicit typecast. Instead we declare a local TMaskEdit variable which sits at the same memory location as the Sender parameter. Note that you have not
created a new TMaskEdit object - you are simply referencing the Sender object in a different way.
Now look at something closer to your own example
procedure TMyForm.MyADOCode;
var qry:TObject;
//like I said, I would rather do var qry:Pointer;
begin
qry:=TOraQuery.Create(nil);
with TOraQuery(qry) do
begin
SQL.Add('Delete from Test 2');
ExecSQL;
Clear;
end;
end;
I am not entirely clear just what you are trying to do but I hope this helps you make sense of Delphi typecasts.
There are a number of miscellaneous points to emphasize
- Code such as this should normally go inside a try...finally block and you must ensure that the object you created is freed up once you are done.
- You can reference multiple objects in a with block. For instance
procedure TMyForm.WithExample;
var AllNames:String;
qry:TOraQuery;
ANames:TStringList;
begin
with qry,ANames do
begin
SQL.Add('Delete from Test 2');//references the SQL property of qry
AllNames:=Text;//references the Text property of ANames
ExecSQL;//calls the ExecSQL method of qry
Clear;//clears qry
end;
end;
When you have such code Delphi scoping rules come into play. The first three lines of code raise no issues. The fourth one, Clear, is rather more complicated. Remember that both the
TOraQuery and the
TStringList classes have a
Clear method. So which of the two objects will get cleared? Given the order in which we have specified them in the
with block it will be
qry. This can lead to very hard to trace bugs so be careful when writing such statements!