User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 373,930 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,255 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser:

delphi object casting

Join Date: Sep 2007
Posts: 61
Reputation: ExplainThat is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
ExplainThat ExplainThat is offline Offline
Junior Poster in Training

Re: delphi object casting

  #2  
Dec 14th, 2007
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!
Reply With Quote  
All times are GMT -4. The time now is 6:00 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC