I have made a few TCollection sets before, and never ran across this problem before, and it has me puzzled.
When I open the collection editor on my component, I am able to add new collection items, set their properties, etc. Even when I close the property editor and re-open it, my collection items are there and have all the values properly.
However, then I save my form, close the form, re-open it, go back to that component and open the collection editor, and those items are gone.
Here's my code for these collections:
And the implementation:
JD Solutions
When I open the collection editor on my component, I am able to add new collection items, set their properties, etc. Even when I close the property editor and re-open it, my collection items are there and have all the values properly.
However, then I save my form, close the form, re-open it, go back to that component and open the collection editor, and those items are gone.
Here's my code for these collections:
Code:
//Event triggered when a specific command has been executed
TJDScktSvrCmdEvent = procedure(Sender: TObject; Socket: TJDServerClientSocket;
const Data: TStrings) of object;
//Represents a collection of different possible commands
TSvrCommands = class(TCollection)
private
fOwner: TJDServerSocket;
function GetItem(Index: Integer): TSvrCommand;
procedure SetItem(Index: Integer; Value: TSvrCommand);
public
constructor Create(ASocket: TJDServerSocket);
destructor Destroy;
procedure DoCommand(const Socket: TJDServerClientSocket; const Cmd: Integer; const Data: TStrings);
function Add: TSvrCommand;
property Items[Index: Integer]: TSvrCommand read GetItem write SetItem;
end;
//Represents a collection item of a specific command which can be executed
TSvrCommand = class(TCollectionItem)
private
fName: String; //Unique Name - user may change to any string, as long as it's not already being used
fID: Integer; //Unique ID - user may change to any integer, as long as it's not already being used
fOnCommand: TJDScktSvrCmdEvent;
//Property Control
procedure SetID(Value: Integer);
procedure SetName(Value: String);
protected
function GetDisplayName: String; override;
public
procedure Assign(Source: TPersistent); override;
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
published
property ID: Integer read fID write SetID;
property Name: String read fName write SetName;
property OnCommand: TJDScktSvrCmdEvent read fOnCommand write fOnCommand;
end;
And the implementation:
Code:
{ TSvrCommands }
function TSvrCommands.Add: TSvrCommand;
begin
Result:= TSvrCommand(inherited Add);
end;
constructor TSvrCommands.Create(ASocket: TJDServerSocket);
begin
inherited Create(TSvrCommand);
Self.fOwner:= ASocket;
end;
destructor TSvrCommands.Destroy;
begin
inherited Destroy;
end;
//Loops through commands to find the one it needs to execute
procedure TSvrCommands.DoCommand(const Socket: TJDServerClientSocket;
const Cmd: Integer; const Data: TStrings);
var
X: Integer;
C: TSvrCommand;
F: Bool;
begin
F:= False;
for X:= 0 to Self.Count - 1 do begin
C:= GetItem(X);
if C.ID = Cmd then begin
F:= True;
if assigned(C.fOnCommand) then
C.fOnCommand(Self, Socket, Data);
Break;
end;
end;
if not F then begin
//Command not found - raise exception
end;
end;
function TSvrCommands.GetItem(Index: Integer): TSvrCommand;
begin
Result:= TSvrCommand(inherited Items[Index]);
end;
procedure TSvrCommands.SetItem(Index: Integer; Value: TSvrCommand);
begin
inherited Items[Index]:= Value;
end;
{ TSvrCommand }
procedure TSvrCommand.Assign(Source: TPersistent);
begin
inherited;
end;
constructor TSvrCommand.Create(Collection: TCollection);
begin
inherited Create(Collection);
//Not creating anything
end;
destructor TSvrCommand.Destroy;
begin
//Not freeing anything
inherited Destroy;
end;
function TSvrCommand.GetDisplayName: String;
begin
Result:= Name;
end;
procedure TSvrCommand.SetID(Value: Integer);
begin
fID:= Value;
end;
procedure TSvrCommand.SetName(Value: String);
begin
fName:= Value;
end;
JD Solutions