Searching within a record

nikif
nikif used Ask the Experts™
on
Hello,
I face a problem in developping a routing. I wonder if it could be coded in a simpler way.

I use D6

Casting of the program is like:

type
  pPerson = ^TPerson;
  TPerson = record
              Name   : string;
              Age,
              Weight,
              Height : integer;
            end;

var
  PersonList: TList;
  aRec      : TPerson;
  p         : pPerson;
  j, N      : integer;
  s         : string;

// I create PersonList, then I add some persons using:
PersonList := TList.Create;
new (p);
p^ := aRec;
PersonList.Add (p);
{
N is a parameter which holds (according to circumstances) age, weight or height.
It serves to identify persons.

The question is:
while scanning PersonList how I could use a _single_ procedure to reach Age, Weight or Height, evaluating N?
}
// Scanning PersonList
p^ := PersonList.Items [j];
aRec := p^;
// 1. N refers to age. So I want to get aRec.Name by checking Age
if aRec.Age = N then s := aRec.Name;    // [1]
// 2. In other cases (if N refers to weight) I will get Name by checking Weight
if aRec.Weight = N then s := aRec.Name; // [2]
// 3. In other cases (may N refer to height) I wish to get Name by checking Height
if aRec.Height = N then s := aRec.Name; // [3]

{ Is possible all above statements under [1], [2] and [3] to get coded into one single procedure like: }

procedure CheckField (ItemX: integer);
// ItemX refers to Age, Weight or Height
begin
  if ItemX = N then s := aRec.Name;
end;

// this proc might be called as follows:
CheckField (aRec.Age);
CheckField (aRec.Weight);
CheckField (aRec.Height);

{I wonder if this might be done using reserved word "as" like
// with Sender as TLabel do ...
// with ItemX as integer do ...
// or like statement record.field.asInteger ...

A sure solution is to check separately these three fields as in [1], [2], [3]
I am wondering if there is a smarter solution
}

Another minor point is:
On exiting must I remove all nodes before free-ing the list or does not matter?

for j := 0 to PersonList.Count - 1 do
begin
   ...
    FreeMem (p); // is this obligatory?
end;
PersonList.Free; // of course free object




Thank you for your attention
Nikiforos
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software architect
Top Expert 2012
Commented:
Simplest solution is to send param search type in your request. This should be like:

const
  CHK_FTYPE_NAME = 1;
  CHK_FTYPE_WEIGHT = 2;
  CHK_FTYPE_HEIGHT = 3;
  CHK_FTYPE_AGE = 4;
...
procedure CheckField (ItemX: Variant; ftType: Integer);
begin
  case ftType of
    CHK_FTYPE_NAME:
      if ItemX = aRec.Name then s := aRec.Name;

    CHK_FTYPE_WEIGHT:
      if ItemX = aRec.Weight then s := aRec.Name;

    CHK_FTYPE_HEIGHT:
      if ItemX= aRec.Height then s := aRec.Name;

    CHK_FTYPE_AGE:
      if ItemX= aRec.Age then s := aRec.Name;
  end;  
end;

Open in new window


call procedure with:

CheckField(p.Age, CHK_FTYPE_AGE)

Use of "as" is not recommended because you have few property defined as Integer.

Author

Commented:
Thanks a lot. It's quiet clear.

About
    FreeMem (p);

What do you advise, is this obligatory before calling Free?

Nikif

Commented:
To your minor point:

Basically you have to free the memory of your records, because TList only handles pointers and does not know of any types...

I recommend declaring  TPerson as an object (TPerson=class(TObject))

Use TObjectList, which has a property OwnsObjects, to control memory of its objects...
Contnrs.TObjectList.OwnsObjects
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Sinisa VukSoftware architect
Top Expert 2012

Commented:
your code should be:

for j := 0 to PersonList.Count - 1 do
begin
   ...
    p := PersonList.Item[j];
    FreeMem(p); // is this obligatory?
    PersonList.Item[j] := nil;
end;
PersonList.Free; // of course free object

Open in new window


... is good and is required. For each allocation you need release (FreeMem).
Release list doesn't release allocated date on each item.

Author

Commented:
I see...
TObject offers much more.
thanks again
Nikiforos

Commented:
Wonder why you did not split the points for my suggestion to use TObject/TobjectList????

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial