Question

In my Question: How to use “Sender” parameter with “As” operator for more then one class at a time

I choose the Remy Lebeau's Answer because it was the most dynamic tech for most situations like that. It uses the RTTI TypInfo Class.

But as I was using this class, another problem came: How do we Set a sub-property value?

function TRemote.UpdateQuery(DataSet: TDataSet; SQL: String): Boolean;
var
  PropInfo: PPropInfo;
begin
{ atualiza o código SQL padrão de um dataSet de consulta tipo View }
  PropInfo := GetPropInfo(DataSet, 'SQL', []);
  if not Assigned(PropInfo) then
  begin
    Result := False;
    Exit;
  end;
  try
    DataSet.Close;
    SetPropValue(DataSet, PropInfo, SQL);
    DataSet.Open;
    Result := True;
  except
    Result := False;
  end;
end;

Example: I have a TIBQuery and I want to update the text of the SQL property. But the SQL property is a TStrings class, so I have to use SQL.Text. In the code above, It will raise an error "Invalid Property Type" because I got a TStrings and later I try to Set a normal String.

How to reach the SQL.Text using GetPropInfo? Is there a common ancestor of TIBQuery and TZQuery that has the SQL property, so I can change to, instead of the TDataSet in the function parameter?

Was it helpful?

Solution

The TStrings.Text property is not accessible via RTTI in Delphi 2006. Even if it were, you do not need to use RTTI to access it anyway. Since you know the SQL property is a TStrings object, you can simply retreive the actual object pointer from the property and type-cast it to a TStrings pointer, then you can do whatever you need to do with that object, eg:

function TRemote.UpdateQuery(DataSet: TDataSet; SQL: String): Boolean; 
var 
  PropInfo: PPropInfo; 
  SQLObj: TStrings;
begin 
  Result := False; 
  try 
    PropInfo := GetPropInfo(DataSet, 'SQL', [tkClass]); 
    if not Assigned(PropInfo) then Exit; 
    SQLObj := TStrings(GetObjectProp(DataSet, PropInfo, TStrings));
    if not Assigned(SQLObj) then Exit; 
    DataSet.Close; 
    SQLObj.Text := SQL; 
    DataSet.Open; 
    Result := True; 
  except 
  end; 
end; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top