Board index » delphi » If a class contains member variables which are objects like tstringlist do you have to call the create method for each member var

If a class contains member variables which are objects like tstringlist do you have to call the create method for each member var

Hi

Im a bit confused

I have a class

type
    TSupplierList = class
    names : tstringlist;
end;

var
    mylist : tsupplierlist;

Can I do the following ?

begin
        mylist := tsupplierlist.create;
        mylist.names.clear;
        mylist.names.add( 'First Name' );
end;

I get an error everytime. Is the .create method propogated up all the member
variables of the class. I cant seem to get this to work. A fundamental
misunderstanding of mine ?

 

Re:If a class contains member variables which are objects like tstringlist do you have to call the create method for each member var


Quote
Dids wrote in message <364aaba...@london.netkonect.net>...
>Hi

>Im a bit confused

>I have a class

>type
>    TSupplierList = class
>    names : tstringlist;
>end;

>var
>    mylist : tsupplierlist;

>Can I do the following ?

>begin
>        mylist := tsupplierlist.create;
>        mylist.names.clear;
>        mylist.names.add( 'First Name' );
>end;

>I get an error everytime. Is the .create method propogated up all the
member
>variables of the class. I cant seem to get this to work. A fundamental
>misunderstanding of mine ?

Yes, but understandable if you're used to using the Form Designer.

As you guessed, you need to explicitly create the "names" stringlist
in the constructor of your TSupplierList class, and free it in the
destructor.

--
Jeremy Collins

Re:If a class contains member variables which are objects like tstringlist do you have to call the create method for each member var


In article <364aaba...@london.netkonect.net>, "Dids"

Quote
<spam.me.not.di...@studio.netkonect.co.uk> writes:
>I get an error everytime. Is the .create method propogated up all the member
>variables of the class.

If you do not override the Create method of the parent then the inherited
Create will be called up the ancestor route. And that of course know nothing
about a TStringList. I think non-multiple-inheritance (Delphi brand of OO)
implies this behaviour.

Unless you code it in the overiding Create method then any newly incorporated
objects in your descendant class are not created. That's why you got a GPF, the
TStringList has to be created. You would normally code :-

type
  TSupplierList = class
    private
      FNames : TStringList;
    public
      property Names TStringList read FNames write FNames;
      constructor Create; override;
      destructor Destroy; override;
  end;

constructor TSupplierList.Create;
begin
  inherited Create;
  FNames := TStringList.Create;
end;

destructor TSupplierList.Destroy;
begin
  FNames.Free;
  inherited Destroy;
end;

 . . .and only then . . .

begin
        mylist := tsupplierlist.create;
        mylist.names.clear;
        mylist.names.add( 'First Name' );
end;

BTW What's this "member variables of the class" - you haven't been using C++
have you - {*word*194} {*word*194} <gg>.

Alan Lloyd
alangll...@aol.com

Re:If a class contains member variables which are objects like tstringlist do you have to call the create method for each member var


Designate the field 'names' (convention is to precede with F) as private:

TSupplierList = class
private
   FNames: TStringList;
end;

The reason for your problem is that you must create FNames in the constructor
for TSupplierList (this won't happen automatically) and then free it in an
overridden implementation of the destructor. You don't need to call
FNames.Clear immediately after instantiation - there's nothing in it anyway

Charles Johnson
if you need more code, give us a shout

Other Threads