Interview Questions

Why do I get compile errors acessing the Sender object in events?

Delphi Interview Questions


(Continued from previous question...)

Why do I get compile errors acessing the Sender object in events?

If you look at the declaration, the Sender object is of type TObject, which is the class from which (almost) all other objects are derived. You're probably trying to access a property that isn't defined in TObject, like Text or Caption or something. For this reason, "Sender.Text" will fail, but if (for example) you know that the sender is of type TEdit, then you could use "(Sender As TEdit).Text". If you aren't certain that the Sender object will always be the same type, you can check it with "if (Sender is TEdit) then < blah> ;". See section 5.14.

(Continued on next question...)

Other Interview Questions