Board index » delphi » TStringList.AddObjects('acting screwy', TNeedHelp)

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

 

Re:TStringList.AddObjects('acting screwy', TNeedHelp)


Quote
"trexler.randy" <trexler.ra...@MCI2000.com> wrote:
>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;

<SNIP><SNIP>

Doesn't the AddObject() method take an Integer as the optional second
parameter?   An index of sorts?

Rkr

- Show quoted text -

Quote
>TIA,
>Randy Trexler
>Systems Analyst/Programmer

Re:TStringList.AddObjects('acting screwy', TNeedHelp)


I think you're probably using Delphi 1 and using Evaluate/Modify in the
de{*word*81}.  It doesn't work well at all.  In particular, it can't understand
many kinds of complicated expressions.  Try setting the stuff you're
looking at into temporary variables and look at the variables.  Chances are
the values will look fine.

This sucks so bad in Delphi 1 I've gone to the lengths of writing my own
dump routines.

Does anyone know of a fix for this, by the way?

trexler.randy <trexler.ra...@MCI2000.com> wrote in article
<01bc187f$9100e740$214437a6@0303150162144370>...

Quote
> 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.
[snip]
> 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!?!

Re:TStringList.AddObjects('acting screwy', TNeedHelp)


On 12 Feb 1997 00:56:01 GMT, "trexler.randy"

Quote
<trexler.ra...@MCI2000.com> wrote:
>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.

The snippets of code that you posted look right, so the problem
probably lies elsewhere. strip out all the extraneous code from your
project and reduce it to the smallest example that manifests your
problem. Post that code without editing it. Without seeing your actual
code, we can only guess...
--
Ray Lischner, Tempest Software, Inc., Corvallis, Oregon, USA
Author of Secrets of Delphi 2 (http://www.tempest-sw.com/secrets/)

Re:TStringList.AddObjects('acting screwy', TNeedHelp)


Well, most of you were correct; it was the Delphi De{*word*81} showing garbage.
If I create another category type variable and set the Stringlist's object
into that new variable; the correct values show.  Strange.....but I
appreciate everyone's replies!!!

--
Randy Trexler
"If your not playing, your not learning!"

Email: trexler.ra...@mci2000.com

Other Threads