I am in the process of creating a Datasnap Server and Client set, with which to eventually connect (server) to several different Databases. For the server I created a test class that I call TMember. The server and client uses this Unit (uMember).
On the server I placed a function that creates and returns a TMember to the requesting Client machine. Some of the data within a TMember object had been collected from a SQL Server DB.
On the Client side I created a Global variable (on my Client main form) of type TObjectList. Every time I fire the Create New Member function, the object is created on the server and successfully sent back down to the client. I can access the object methods and properties.
My problem, however, is iterating over this TObjectList collection. No matter which way I do it, I only get AVs. Here are my code attempts. Can anyone perhaps indicate where I am messing up?
Thank you very much in advance!
Here is the TMember interface:
type
TMember = class
FFirstName : String;
FLastName : String;
FFullname : String;
FAddress : String;
private
{private declarations}
public
{public declarations and properties}
constructor Create(fname, lname : String); overload;
constructor Create(otherMember : TMember); overload;
destructor Destroy;
procedure SetFName(const Value: String);
procedure SetFullname(const Value: String);
procedure SetLName(const Value: String);
function GetFullname : String;
function ToString : String;
procedure ChangeAddr(Addr : String);
property Name : String read FFirstName write SetFName;
property Surname : String read FLastName write SetLName;
property Fullname : String read GetFullname write SetFullname;
end;
This code is used to create a new TMember object from the Server:
procedure TfrmMainClient.btnCreateMemClick(Sender: TObject);
var
Server : TServerMethodsClient;
newMem : TMember;
i : Integer;
begin
try
Server := TServerMethodsClient.Create(DM1.ServerConn.DBXConnection);
newMem := Server.GetMember(edtFName.Text, '');
if Assigned(newMem) then begin
AllMembers.Add(newMem);
Listbox1.Items.Add(newMem.GetFullname);
end;
finally
Server.Free;
end;
end;
So, after a while there are several objects floating around in the Client's memory. Now I would like to access them from the Listbox (as if it is a "menu" to select a TMember from). The idea is/was to record the Listbox's ItemIndex and send that to the AllMember TObjectList object collection to access the TMember sitting at that Index. This creates a lovely little AV. So, then I tried a simple iteration over the ObjectList using a basic for..do loop filling out a Listbox. Same result. I also tried a for..in loop, but to no avail.
I am pretty sure it is something silly that I am simply missing...
Here is attempt # 1:
procedure TfrmMainClient.btnRefreshClick(Sender: TObject);
var
nextMember : TMember;
begin
for nextMember in AllMembers do
begin
Listbox1.Items.Add(nextMember.GetFullname);
end;
end;
Here is attempt # 2:
procedure TfrmMainClient.btnRefreshClick(Sender: TObject);
var
nextMember : TMember;
i : Integer;
begin
for i := 0 to AllMembers.Count - 1 do
begin
nextMEmber := TMember.Create(AllMembers[i]);
Listbox1.Items.Add(nextMember.GetFullname);
end;
end;
Here is attempt # 3:
procedure TfrmMainClient.btnRefreshClick(Sender: TObject);
var
nextMember : TMember;
i : Integer;
begin
for i := 0 to AllMembers.Count - 1 do
begin
nextMember := AllMembers.Items[i];
Listbox1.Items.Add(nextMember.GetFullname);
end;
end;
I trust someone could assist...
Edit 1:
Change this
procedure TfrmMainClient.btnRefreshClick(Sender: TObject);
var
i: Integer;
Item : TPair<Integer, TMember>;
ActiveMember : TMember;
Key : Integer;
begin
Listbox1.Clear;
for i := 0 to AllMembersKeys.Count - 1 do
begin
try
ActiveMember := TMember.Create('','');
//Get the MemberID as a Key stored in AllMembersKeys
Key := AllMembersKeys[i];
//Now try get this TMember record from the AllMembers collection
if AllMembers.TryGetValue(Key, ActiveMember) then
if ActiveMember <> nil then
Listbox1.Items.Add(ActiveMember.GetFullname)
else
Listbox1.Items.Add('Could not locate TMember with Key: ' + IntToStr(Key));
finally
ActiveMember.Free;
end;
end;
end;
to
procedure TfrmMainClient.btnRefreshClick(Sender: TObject);
var
Key : Integer;
begin
Listbox1.Clear;
for Key in AllMembers.Keys do
begin
Listbox1.Items.Add(AllMembers[Key].GetFullname);
end;
end;