Question

I'm doing a simple class definition in Delphi and I wanted to use a TStringList in the class & it's constructor (so everytime you create an object, you pass it a StringList and it does some magic stuff to the StringList data, copying the string list to it's own internal string list). The problem I get is that when I try to declare what it "uses" before the class definition (so it knows how to handle the TStringList), it fails on compile. But without that, it doesn't know what a TStringList is. So it seems to be a scoping issue.

Below is a (very simplified) class definition, similar to what I'm trying to do. Can someone suggest how I can make this work and get the scoping right?

I tried adding the uses statements at the project level as well, but it still fails. I wonder what I need to do to get this right.

unit Unit_ListManager;

interface

type
TListManager = Class

private
  lmList   : TStringList;
  procedure SetList;


published
  constructor Create(AList : TStringList);
end;

implementation

uses
  SysUtils,
  StrUtils,
  Vcl.Dialogs;

  constructor TBOMManager.Create(AList : TStringList);
  begin
    lmList := TStringList.Create;
    lmList := AListList;
  end;

  procedure SetPartsList(AList : TStringList);
  begin
     lmList := AListList;
     ShowMessage('Woo hoo, got here...');
  end;
end.

Kind Regards

Was it helpful?

Solution

You didn't show where exactly you were adding the unit reference, but I'm betting it was the wrong place. Take note of the additional code between interface and type.

I've also corrected your definition of the constructor, which you had placed in published instead of public. Only property items belong in the published section.

unit Unit_ListManager;

interface

uses
  Classes,
  SysUtils,
  StrUtils,
  Vcl.Dialogs;    

type
TListManager = Class
private
  lmList   : TStringList;
  procedure SetList;    
public
  constructor Create(AList : TStringList);
end;

implementation

constructor TListManager.Create(AList : TStringList);
begin
  inherited Create; // This way, if the parent class changes, we're covered!
  // lmList := TStringList.Create; This would produce a memory leak!
  lmList := AListList;
end;

procedure TListManager.SetList;
begin
// You never provided an implementation for this method
end;

end.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top