I use common procedures for all Virtual Treeviews (TVirtualStringTree) so I only have 1 code to maintain, like for OnClick I use Common_VST_OnClick which all VST controls has set:
procedure TForm1.Common_VST_OnClick(Sender: TObject);
And to execute code based on which VST calls this on click procedure, I realized I use many different ways to recognize which control is Sender:
if Sender = VST1 then
if Sender.Name = VST1.Name then
if TVirtualStringTree(Sender) = VST1 then
if TVirtualStringTree(Sender).Name = VST1.Name then
if TVirtualStringTree(Sender).Name = 'VST1' then
The last is probably worst as the name is hardcoded, so I'm trying to only use 1 type of identification, in all procedures.
What is the best way to identify which control is Sender?
You should prefer the test that uses object identity. That is, the first test in your question:
if Sender = VST1 then
An object reference such as Sender
or VST1
is the address of the object. If two such addresses are equal, then the references point to same object. And vice versa.
The tests based on control name can work but are brittle. It is possible for multiple controls to have the same name. It is possible to change the control name but not update all the uses of the name in the program.
As for the type casting option
if TVirtualStringTree(Sender) = VST1 then
the type cast has no impact on object identity and so is needless. Don't ever type cast an operand to an object identity test since doing so is spurious.