In regards to the previous question below, some years ago when I
first started learning Delphi, I had an interesting conversation with Cosmin
right here about the various ways one could setup an editor for multiple
pages/files. At the time I was trying to use a record to do much the same
thing as I'm doing now with an object. In either case however it boils down
to whether or not it's worthwhile using a record/object over simply adding
additional instances of A TMemo or TRichEdit. As he pointed out, using the
latter method is much easier as one doesn't need to worry about such things
as resetting the cursor position to where it was before when switching from
one page to another, and with memory as cheap and abundant as it is the
resource savings in using just one edit control may not really be worth all
the effort you would need to put into coding!
In this situation however, the editor must work equally with both
disk-based and BLOB files, often at the same time, and including the ability
to transfer the data from one to the other no matter what the original
source was. So in thus case I decided to go ahead and use an object to hold
all relevant data, and one edit control for all pages.
I got to thinking however that unless I actually needed to include
methods in this object type, I might be better off using a record instead.
Am I correct in that a record having the same number and type of properties
as does an object uses a lot less memory?
Also, though it certainly isn't necessary, I wanted to do this
without using any pointers at all, and so I was attempting to use both a
TStringlist and a TObjectList to hold and then reference each object as one
moves from tab to tab in the editor. I'd always used pointers for such
things in the past, but I remember seeing a code example somewhere that
showed how one could reference the property fields of an object directly
from within the List that held them. I've looked for this or some other
example and not had any luck., but it seems to me that it involved creating
a wrapper class around the TObjectList so one could write code like this
where the object type the list is holding is a TMemo:
With myObjectList.items[i] do
Begin
Name := 'anyname';
Color := clblue;
Font.Color ;= clWhite;
WordWrap := True;
End;
It wasn't until later this afternoon that it hit me I could also use
an array to hold these objects and they automatically allow one to reference
the object properties without having to typecast or use pointers! A quick
look at the Delphi help files proved only how bad they are, and so I googled
around for some examples to see if I was correct, and although I didn't find
exactly what I was looking for, I did learn enough to be able to figure the
rest out on my own.
I haven't used Arrays very much at all...especially dynamic
arrays...which for this situation I think would be best. But I have a few
quick questions I hope someone can answer for me.
First of all, I realize Dynamic Arrays are not really dynamic in the
same sense as strings are in that they won't simply grow in size as required
without the programmer's help. Using Setlength is what does the trick, and
one can call it anytime you need to increase the number of fields required
by the array. And I'm guessing the compiler figures out just how much
memory is required for each field based upon the type of object being placed
into them.
So if I create an array is ii okay to set its length at the start to
just 1? I tried it and it compiles and runs okay but I wondered if there
might be something else that would make doing so a bad idea?
Now when I need to create a new page in the editor, I first create
the new tab, then a new instance of the object type I wrote, and then I call
SetLength and increment its current length to hold another object, and
finally I assign the object to the array. All this I've tested and it works
great but it's freeing the resources used I'm not so sure about.
When I'm ready to close the editor, I must first free and nil each
instance of my object that I had created at runtime, correct? But what if
when I create each object I set the form as the object's owner? Would they
then be freed like any other component when the form itself is freed? What
about making the object's owner the array itself? Is this possible and
would there be any advantage or disadvantage to doing so? And finally, is
it true that no matter how many fields an array might contain, simply
setting the array to nil will free it properly, or do I have to iterate thru
and delete each field first? Is there anyway, like with an ObjectList, to
make the array responsible for the objects it references?
Thanx in advance for any info on this topic.
from Robert Meek dba Tangentals Design CCopyright 2006
"When I examine myself and my methods of thought, I come to the conclusion
that the gift of Fantasy has meant more to me then my talent for absorbing
positive knowledge!"
Albert Einstein
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Robert Meek
Sent: Friday, March 03, 2006 11:26 AM
To: [email protected]
Subject: Objects and Lists
I've been trying to write a simple code editor that allows for
multiple files to be opened at the same time, and using only one TMemo or
TRichedit to do so. My intention was to create an object like so:
type
TMainObject = Class(TObject)
FName : String;
FFileName : String;
FLines : TStrings;
FEnc : Boolean; // for Encryption
FROnly : Boolean; // for ReadOnly
FTag : Integer; // ?
End;
As well as some simple local to the unit variables:
Var
MyObj : TMainObject;
ObjList : TObjectList;
ObjCount : Integer;
And as the editor is created or one clicked on "New", or "Open" an
instance of MyObj would be created and added to a TObjectList so as to keep
track of them. At the same time a new TAB would be added to the tabcontrol
so the user could move from tab to tab, with each move storing the current
lines of the editor in the former tab's object and then loading the lines of
the editor from the property of the new tab's object. In the case of a
"New" instance all properties would be '' or False except for the FName
which would be "Untitled", and the FTag which would equal the index of the
ObjectList Item it is added as, and which also would equal the tabIndex of
the Tab it belongs to.. Another var would keep track of the count from
outside the Object.
The one rule I wanted to follow while doing this is that I use NO
pointers whatsoever. But I cannot seem to find the correct syntax for
working with the object in the ObjectList I need at any particular time
without them, or without having provided a number of pre-named Vars of the
Object type so that I can reference them by name! Yet I know I've seen this
done somewhere! Could it have been via a special wrapper class around the
TObjectList? Is there an easier way of doing this? Note: The reason I
want to do this with objects is that I can then more easily access for
read/write to similar fields in a dB table when necessary without having to
create a stream or tempfile.
from Robert Meek dba Tangentals Design CCopyright 2006
"When I examine myself and my methods of thought, I come to the conclusion
that the gift of Fantasy has meant more to me then my talent for absorbing
positive knowledge!"
Albert Einstein
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi