Re: What does this do: TMyClass(AClass.member).Member?




JamesR wrote:
Hi

I've come across this sort of code:

TUserData = class
public
FRequestCount: Integer;
end;

......

begin
.... IntToStr(TUserData(AContext.Data).FRequestCount);

Now, AContext is a class and .Data is a property of type TObject. What
exactly is going on here? I don't understand what
TUserData(AContext.Data) is doing. Type casting this some how? or..?
and then the .FRequestCount?


AContext.Data could be of one of any class but is known by the coder to
be a TUserData or its descendant.

TUserData(AContext.Data) does a simple (ie not type-checked) typecast
to a class of type TUserData.

TUserData(AContext.Data).FRequestCount accesses the FRequestCount field
of the the object typecast object.

In your example AContext.Data would most likely be a parameter of a
standard type of procedure/function. Typically this type of construct
is found in an event handler, which has a parameter Sender of type
TObject in a procedure of type TNotify. The Sender parameter is often
of some type of control, TObject is the lowest common object so it
covers _any_ object. When one writes an event handler one must typecast
Sender to be the object it actually is, so that one can access the
particular property one desires of that particular Sender.

Alan Lloyd

.