Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-06

GetPropValue and Unknown property

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Antonio Sanguigni

Hi all,

I'm trying to derive a new class TSortFtpSend from synapse TFtpSend class, which implements a quicksort algo based on published property passed to this method.
I'm having some troubles with this code because GetPropValue seems does not find the published property I need:

function TSortFtpSend.DoCompareByProps(AItem1: Pointer; AItem2: Pointer;
  const ASortProps: array of string; AAscendingOrder: Boolean): integer;
var
  i : integer;
  lsPropName : string;
  lValue1 : variant;
  lValue2 : variant;
begin
  result := 0;
//  try
    for i := Low(ASortProps) to High(ASortProps) do
    begin
      lsPropName := ASortProps[i];
      writeln(TFTPListRec(AItem1).FileName);
      writeln(TFTPListRec(AItem1).FileSize);
      lValue1 := GetPropValue(TFTPListRec(AItem1),lsPropName); // <==
Here I got the Unknown property FileSize exception
      lValue2 := GetPropValue(TFTPListRec(AItem2),lsPropName);

[CUT]

It seems GetPropValue cannot find published property FileSize but the writeln statement works well printing the exact value, and of course the properties are published. Someone has an idea why that is not working here ?

Thanks
Antonio

Antonio Sanguigni alias slapshot ---------------------------------------------------------------------- GioveLUG (Linux User Group) - http://www.giovelug.org Edupup (Educational distro) - http://www.pieroni.biz/edupup
Leonardo M. Ramé
to Antonio Sanguigni

How are you filling the ASortProps array? if it is by hand, you have to take care of Case Sensitivity of property names.

You can use this function to know the published properties of any class. Try it inside your DoCompareProps, passing TFTPListRec(AItem1).ClassType as param, if AItem1 is an instance it should list all its properties:

procedure GetClassProperties(AClass: TClass);
(* get published properties into a list *)
var
  PropList:      ppropList;
  ClassTypeInfo: PTypeInfo;
  I:             Integer;
  lProps:        Integer;
begin
  (* Get properties and a pointer to class *)
  lProps := GetTypeData(AClass.ClassInfo)^.PropCount;
  if lProps  0 then
  begin
    (* Properties list to be analyzed *)
    for I := 0 to GetPropList(AClass.ClassInfo, PropList) -1 do
      writeln(PropList[I]^.name);
  end;
end;

Leonardo M. Ramé
http://leonardorame.blogspot.com

Earlier message 50 lines
----- Original Message ---- From: Antonio Sanguigni To: FPC-Pascal users discussions Sent: Tuesday, June 23, 2009 7:41:56 AM Subject: [fpc-pascal] GetPropValue and Unknown property Hi all, I'm trying to derive a new class TSortFtpSend from synapse TFtpSend class, which implements a quicksort algo based on published property passed to this method. I'm having some troubles with this code because GetPropValue seems does not find the published property I need: function TSortFtpSend.DoCompareByProps(AItem1: Pointer; AItem2: Pointer; const ASortProps: array of string; AAscendingOrder: Boolean): integer; var i : integer; lsPropName : string; lValue1 : variant; lValue2 : variant; begin result := 0; // try for i := Low(ASortProps) to High(ASortProps) do begin lsPropName := ASortProps[i]; writeln(TFTPListRec(AItem1).FileName); writeln(TFTPListRec(AItem1).FileSize); lValue1 := GetPropValue(TFTPListRec(AItem1),lsPropName); // <== Here I got the Unknown property FileSize exception lValue2 := GetPropValue(TFTPListRec(AItem2),lsPropName); [CUT] It seems GetPropValue cannot find published property FileSize but the writeln statement works well printing the exact value, and of course the properties are published. Someone has an idea why that is not working here ? Thanks Antonio -- Antonio Sanguigni alias slapshot ---------------------------------------------------------------------- GioveLUG (Linux User Group) - http://www.giovelug.org Edupup (Educational distro) - http://www.pieroni.biz/edupup
Mattias Gärtner
to Leonardo M. Ramé

Zitat von "Leonardo M. Ramé" :

Show quoted text 28 lines
How are you filling the ASortProps array? if it is by hand, you have to take care of Case Sensitivity of property names. You can use this function to know the published properties of any class. Try it inside your DoCompareProps, passing TFTPListRec(AItem1).ClassType as param, if AItem1 is an instance it should list all its properties: procedure GetClassProperties(AClass: TClass); (* get published properties into a list *) var PropList: ppropList; ClassTypeInfo: PTypeInfo; I: Integer; lProps: Integer; begin (* Get properties and a pointer to class *) lProps := GetTypeData(AClass.ClassInfo)^.PropCount; if lProps 0 then begin (* Properties list to be analyzed *) for I := 0 to GetPropList(AClass.ClassInfo, PropList) -1 do writeln(PropList[I]^.name); end; end;

This will create a mem leak. Call GetPropList only once and FreeMem the PropList at the end.

Mattias

Powered by NetMail
Leonardo M. Ramé
to Mattias Gärtner

Yes, Mattias is right, I copied-pasted-removed some stuff from one of my programs and leaved out the FreeMem part.

This is a modified version:

procedure GetClassProperties(AClass: TClass);
(* get published properties into a list *)
var
 PropList:      ppropList;
 ClassTypeInfo: PTypeInfo;
 I:             Integer;
 lProps:       Integer;
begin
 (* Get properties and a pointer to class *)
 lProps := GetTypeData(AClass.ClassInfo)^.PropCount;
 if lProps  0 then
 begin
    (* Properties list to be analyzed *)
   GetPropList(AClass.ClassInfo, PropList);
    for I := 0 to lProps -1 do
      writeln(PropList[I]^.name);
   (* Free memory allocated for PropList *)
   FreeMem(PropList);
 end;
end;

Leonardo M. Ramé
http://leonardorame.blogspot.com

Earlier message 40 lines
----- Original Message ---- From: Mattias Gärtner To: Sent: Tuesday, June 23, 2009 10:20:10 AM Subject: Re: [fpc-pascal] GetPropValue and Unknown property Zitat von "Leonardo M. Ramé" : > > > > How are you filling the ASortProps array? if it is by hand, you have to take care of Case Sensitivity of property names. > > You can use this function to know the published properties of any class. Try it inside your DoCompareProps, passing TFTPListRec(AItem1).ClassType as param, if AItem1 is an instance it should list all its properties: > > procedure GetClassProperties(AClass: TClass); > (* get published properties into a list *) > var > PropList: ppropList; > ClassTypeInfo: PTypeInfo; > I: Integer; > lProps: Integer; > begin > (* Get properties and a pointer to class *) > lProps := GetTypeData(AClass.ClassInfo)^.PropCount; > if lProps 0 then > begin > (* Properties list to be analyzed *) > for I := 0 to GetPropList(AClass.ClassInfo, PropList) -1 do > writeln(PropList[I]^.name); > end; > end; This will create a mem leak. Call GetPropList only once and FreeMem the PropList at the end. Mattias -- Powered by NetMail
Antonio Sanguigni
to Leonardo M. Ramé

2009/6/23 Leonardo M. Ramé :

Yes, Mattias is right, I copied-pasted-removed some stuff from one of my programs and leaved out the FreeMem part. This is a modified version:

Thanks both guys for your help. The problem was really simple, I mean, the library I'm using has a class with published properties but there is not a {$M+} compiler directive for the RTTI. It is working now after I added it.

Thanks again.
Antonio

Antonio Sanguigni alias slapshot ---------------------------------------------------------------------- GioveLUG (Linux User Group) - http://www.giovelug.org Edupup (Educational distro) - http://www.pieroni.biz/edupup