This is a fairly trivial example. It is the preferred solution if you want to iterate over a set of specific objects and set properties in a consistent way without having to write repetitive code referencing specific objects.
var
Temp: TComponent;
begin
for i := ComponentCount - 1 downto 0 do
begin
Temp := Components[i];
if (Temp is TADOQuery) then
begin
if Length(TADOQuery(Temp).ConnectionString)>0 then
raise Exception.Create('UnitTest(1): The use of a data connection string is not supported in this model.' + Temp.Name);
if TADOQuery(Temp).Connection = nil then
raise Exception.Create('UnitTest(2): A default connection object should be specified. ' + Temp.Name);
end;
end;
Some notes:
The code TADOQuery(Temp).Connection is shorthand for casting the TComponent object to an TADOQuery object and accessing its property Connection at the same time. A temporary unnamed TADOQuery object is created on the stack and disappears after the code is executed.
ComponentCount is a property that every object has that is derived from TComponent.