TStringList.AddObjects('acting screwy', TNeedHelp)
If anyone could take a second to look at this and figure out what I'm doing
wrong, I'd really appreciate it. I'm not sure why my AddObjects function
doesn't work right.
I've declared 2 new classes:
TCategory = class(TObject)
ID: Integer;
Name: String[50];
end;
TCategoryList = class(TStringList)
public
procedure LoadFromDatabase(Query: TQuery);
end;
In the implementation of the LoadFromDatabase method for TCategoryList, I'm
trying add a bunch of TCategory objects to each StringList's
(TcategoryList's) object property. But my TCategory object's values are
getting overwritten after I do an AddObject:
procedure TCategoryList.LoadFromDatabase(Query: TQuery);
var
p : integer;
begin
...blah blah blah....
p := AddObject('test',TCategory.Create);
TCategory(Objects[p]).ID := 2;
TCategory(Objects[p]).Name := 'test';
end;
Try this real quick and check out the value of TCategory(Objects[0]).ID and
.Name. They are garbage when they should equal '2' and 'test',
respectively. I've even tried creating a variable of
type TCategory and setting the values before adding the Object to the
TStringList (TCategoryList) (which is basically doing the same thing) and I
still get garbage.
{LoadFromDatabase Method}
...
category := TCategory.Create;
category.ID := 2;
category.Name := 'test'
AddObject('test', category)
in this case, even the original category.ID and .Name values get
overwritten with garbage after the AddObject statement. Any clues what I'm
doing wrong?
NOTE: if I take this code out of the subclass method and apply what I'm
trying to do to a just an everyday, ordinary TStringList variable,
everything works correctly. It must have something to do with me
subclassing!?!
TIA,
Randy Trexler
Systems Analyst/Programmer