I have many places where I query the type and then access it with a typecast.
Here is an example:
procedure TWebBasisForm.InitDefaultElementID;
var
i: Integer;
s: String;
c: TComponent;
begin
s := StringReplace( self.UnitName, '.', '_', [rfReplaceAll]);
for i := 0 to ComponentCount-1 do begin
c := Components[i];
if (c is TWebLabel) and (TWebLabel( c).ElementID = '') then TWebLabel( c).ElementID := s+'_'+TWebLabel( c).Name
else if (c is TWebEdit) and (TWebEdit( c).ElementID = '') then TWebEdit( c).ElementID := s+'_'+TWebEdit( c).Name
else if (c is TWebSpinEdit) and (TWebSpinEdit( c).ElementID = '') then TWebSpinEdit( c).ElementID := s+'_'+TWebSpinEdit( c).Name
else if (c is TWebCheckBox) and (TWebCheckBox( c).ElementID = '') then TWebCheckBox( c).ElementID := s+'_'+TWebCheckBox( c).Name
else if (c is TWebRadioButton) and (TWebRadioButton(c).ElementID = '') then TWebRadioButton(c).ElementID := s+'_'+TWebRadioButton(c).Name
else if (c is TWebComboBox) and (TWebComboBox( c).ElementID = '') then TWebComboBox( c).ElementID := s+'_'+TWebComboBox( c).Name
else if (c is TWebMemo) and (TWebMemo( c).ElementID = '') then TWebMemo( c).ElementID := s+'_'+TWebMemo( c).Name
else if (c is TWebListControl) and (TWebListControl(c).ElementID = '') then TWebListControl(c).ElementID := s+'_'+TWebListControl(c).Name
else if (c is TWebButton) and (TWebButton( c).ElementID = '') then TWebButton( c).ElementID := s+'_'+TWebButton( c).Name
else if (c is TWebHTMLDiv) and (TWebHTMLDiv( c).ElementID = '') then TWebHTMLDiv( c).ElementID := s+'_'+TWebHTMLDiv( c).Name
else if (c is TWebHTMLSpan) and (TWebHTMLSpan( c).ElementID = '') then TWebHTMLSpan( c).ElementID := s+'_'+TWebHTMLSpan( c).Name;
end;
end;
When I debug it later in the browser, WebLabel sometimes becomes TLabel and it no longer works.
Here is the code from browser debug:
procedure TBasisForm.InitDefaultElementID;
var
i: Integer;
s: String;
c: TComponent;
begin
s := StringReplace( self.UnitName, '.', '_', [rfReplaceAll]);
for i := 0 to ComponentCount-1 do begin
c := Components[i];
if (c is TLabel) and (TLabel( c).ElementID = '') then TLabel( c).ElementID := s+'_'+TWebLabel( c).Name
else if (c is TEdit) and (TEdit( c).ElementID = '') then TEdit( c).ElementID := s+'_'+TWebEdit( c).Name
else if (c is TSpinEdit) and (TSpinEdit( c).ElementID = '') then TSpinEdit( c).ElementID := s+'_'+TWebSpinEdit( c).Name
else if (c is TCheckBox) and (TCheckBox( c).ElementID = '') then TCheckBox( c).ElementID := s+'_'+TWebCheckBox( c).Name
else if (c is TRadioButton) and (TRadioButton(c).ElementID = '') then TRadioButton(c).ElementID := s+'_'+TWebRadioButton(c).Name
else if (c is TComboBox) and (TComboBox( c).ElementID = '') then TComboBox( c).ElementID := s+'_'+TWebComboBox( c).Name
else if (c is TMemo) and (TMemo( c).ElementID = '') then TMemo( c).ElementID := s+'_'+TWebMemo( c).Name
else if (c is TListControl) and (TListControl(c).ElementID = '') then TListControl(c).ElementID := s+'_'+TWebListControl(c).Name
else if (c is TButton) and (TButton( c).ElementID = '') then TButton( c).ElementID := s+'_'+TWebButton( c).Name
else if (c is THTMLDiv) and (THTMLDiv( c).ElementID = '') then THTMLDiv( c).ElementID := s+'_'+TWebHTMLDiv( c).Name
else if (c is THTMLSpan) and (THTMLSpan( c).ElementID = '') then THTMLSpan( c).ElementID := s+'_'+TWebHTMLSpan( c).Name;
end;
end;
Sometime is TWebLabel changed to TLabel and sometimes not.
And my code does not work.
I have a lot of places with type queries. I'm not sure which ones work and which ones don't.
Is there a rule here?