Some understanding here is useful.

It is not possible to create numerous objects with the same name.

When you create an instance of a class (an object) you are doing several
things:
1. Delphi reserves some memory for you
2. Delphi gives you in the variable on the left hand side, a reference to
that piece of memory.

If you then create another instance of the object using the same variable
name, you have lost access to the bit that you had before. it is still there
but you have no way of accessing it, because the variable now points to the
new bit you got. You can do this ten times but at the end you will still
only be able to access the last bit.

To get around this little problem when you need to keep lists of objects
created dynamically, Delphi provides the TList class. You can use a TList or
make a derivative from it. It would be usual to override the destructor to
ensure all the memory is freed in your objects.

Now, when using a TList the above steps become:

1. Delphi reserves some memory for you
2. Delphi gives you in the variable on the left hand side, a reference to
that piece of memory.
3. You add the value of that variable to a TList object.

Then you can use the methods of TList to access all those dynamic objects
you have created. In the days before OO we used to use linked lists of
pointers to do the same thing. Compared to that a TList is a doddle.

Addressing to your particular situation. A record is not an object. A
Tobject is an object.

I used to use records until I discovered how to use TList to link a whole
lot of dynamically created objects. To give an example I have just updated
an application that uses counters to count numbers in a big report, and do
various calculations etc. It used to use various TStringLists to store all
the counters. I updated it to use OO stuff. This is good because there are
two different types of counter and one is a subclass of the other.

All you have to do is:
* Create an instance of your object e.g. MyObject := TMyObject.Create
* Do something with it...set the fields or something
* Add it to the Objects property of the list box items (which is probably
implemented as a TList) . No typecast needed since everything is descended
from TObject.

Now you can safely reuse MyObject to create another instance, as the
reference is now saved in the list box. (Don't free it though, as it still
points to an instance, but if you wish you can set it to nil without
affecting any instances of your object)

When you have to do something with your object, it looks like this:

MyObject := TMyObject(ListBox1.Items.Objects[X]

You have to typecast it because Delphi stores it as a TObject and won't let
you access any of the methods or properties other than for a TObject, if you
don't do the typecast, also you will get an "invalid type" error from the
compiler.

Note that the Objects property still contains the reference until that list
item is deleted.The question I am not at all sure about is whether Delphi
frees the memory associated with each object reference when the list item is
removed from the list.


> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
> Behalf Of Ross Levis
> Sent: Friday, 6 July 2001 23:33
> To: Multiple recipients of list delphi
> Subject: Re: Listbox items (Was: Re: [DUG]: array of TStringList)
>
>
> Thanks James, Ben & Reg.  It's looks rather messy to me.  I
> didn't realise you
> can create numerous duplicate objects with the same name.  Is there any
> advantage to using a Record object as opposed to a class(TObject)?
>
> Ross.
>
> James Low wrote:
>
> > Thanks Ben ...
> >
> > -----Original Message-----
> > From: Ben Taylor [mailto:[EMAIL PROTECTED]]
> > Sent: 6 July 2001 17:01
> > To: Multiple recipients of list delphi
> > Subject: RE: Listbox items (Was: Re: [DUG]: array of TStringList)
> >
> > > AddObject('Somestring to display', myObject);
> > > myObject.Free; //the list owns a reference to it now
> >
> > you _really_ dont want to do this.. the list now has a pointer to the
> > object, but its still the same object. if you do the free, then the
> > list holds an invalid pointer.
> >
> > > myObject := Listbox1.Objects[ListBox1.Listindex];
> > this wont compile. you'll need to cast the TObject pointer to the
> > correct type:
> >
> > myObject := tRossesObject(Listbox1.Objects[ListBox1.Listindex]);
> >
> > that should work better..
> >
> > there are 'nicer' OO ways to do this but it's friday and i'm going now
> > :-)
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Get personalized email addresses from Yahoo! Mail
> > http://personal.mail.yahoo.com/
> >
> ------------------------------------------------------------------
> ---------
> >     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
> >                   Website: http://www.delphi.org.nz
> > To UnSub, send email to: [EMAIL PROTECTED]
> > with body of "unsubscribe delphi"
> >
> ------------------------------------------------------------------
> ---------
> >     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
> >                   Website: http://www.delphi.org.nz
> > To UnSub, send email to: [EMAIL PROTECTED]
> > with body of "unsubscribe delphi"
>
> ------------------------------------------------------------------
> ---------
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
>

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"

Reply via email to