Create()-probs

Posted by Martin Knappe

Hello people,

I'm new to Pascal and Delphi but what I'm encountering now really goes beyond my capacities. What I'm trying to do in the following snippet is simply create objects of my own TEntry class and then insert them into a list of my own-defined list class (which inherits from TList ; referenced by self here) but it seems like every time I invoke Create I get the same pointer back, so in the end I'm creating only one object!!! Have a look at this one:

var
  p: Pointer;
  i: Integer;
  content: TStrings;
  entry: TEntry;
begin
  p := nil;
  content := TStringList.Create();
  content.LoadFromFile(FileName);
  if content.Count mod 2 = 0 then
  begin
    i := 0;
    while i < content.Count do
      begin
        entry := TEntry.Create();
        if p = Addr(entry) then
          ShowMessage('Same address!!!');
        //some instruction
        Inc(i);
        //some instruction
        Inc(i);
        self.Add(Addr(entry));
        p := Addr(entry);
      end;
...

If you execute that in a program you always get the 'Same address!!!' message. This is not what I intend. I obviously intend to create content.Count/2 objects and have them all added to my list.

What is wrong with that code??? Please help,
best,
Martin

Related Articles and Replies


DelphiLand Discussion Forum