overlook2004
asked on
Chat Room
I'm developing a chat server and I found a useful code here about creating multiple chatrooms.
My questions are how I can use this code? How I can make it functional (Please provide examples - e.g. creating/add/removing/.. rooms).
here is the code which was posted by BlackTigerX:
-------------------------- -----
uses
Classes;
type
TChatRoom = class(TCollectionItem)
private
FTitle: string;
FUsers:TStringList;
procedure SetTitle(const Value: string);
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
function GetUserList:TStringList;
function AddUser(const userName:string):Boolean;
function RemoveUser(const userName:string):Boolean;
//*** chat room title
property Title:string read FTitle write SetTitle;
end;
TChatRooms = class(TCollection)
private
function GetItem(index: Integer): TChatRoom;
procedure SetItem(index: Integer; const Value: TChatRoom);
public
function Add():TChatRoom;
property Items[index:Integer]:TChat Room read GetItem write SetItem;
end;
implementation
{ TChatRooms }
function TChatRooms.Add: TChatRoom;
begin
Result:=TChatRoom(inherite d Add())
end;
function TChatRooms.GetItem(index: Integer): TChatRoom;
begin
Result:=TChatRoom(inherite d GetItem(index))
end;
procedure TChatRooms.SetItem(index: Integer; const Value: TChatRoom);
begin
inherited SetItem(index, Value)
end;
{ TChatRoom }
function TChatRoom.AddUser(const userName:string):Boolean;
begin
FUsers.Add(userName)
end;
constructor TChatRoom.Create(Collectio n: TCollection);
begin
inherited;
FUsers:=TStringList.Create ;
FUsers.Sorted:=True
end;
destructor TChatRoom.Destroy;
begin
FUsers.Free;
inherited;
end;
function TChatRoom.GetUserList: TStringList;
begin
Result:=TStringList.Create ;
Result.Assign(FUsers);
end;
function TChatRoom.RemoveUser(const userName:string):Boolean;
var
idx:Integer;
begin
idx:=FUsers.IndexOf(userNa me);
if (idx>=0) then
FUsers.Delete(idx);
end;
procedure TChatRoom.SetTitle(const Value: string);
begin
FTitle := Value;
end;
My questions are how I can use this code? How I can make it functional (Please provide examples - e.g. creating/add/removing/.. rooms).
here is the code which was posted by BlackTigerX:
--------------------------
uses
Classes;
type
TChatRoom = class(TCollectionItem)
private
FTitle: string;
FUsers:TStringList;
procedure SetTitle(const Value: string);
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
function GetUserList:TStringList;
function AddUser(const userName:string):Boolean;
function RemoveUser(const userName:string):Boolean;
//*** chat room title
property Title:string read FTitle write SetTitle;
end;
TChatRooms = class(TCollection)
private
function GetItem(index: Integer): TChatRoom;
procedure SetItem(index: Integer; const Value: TChatRoom);
public
function Add():TChatRoom;
property Items[index:Integer]:TChat
end;
implementation
{ TChatRooms }
function TChatRooms.Add: TChatRoom;
begin
Result:=TChatRoom(inherite
end;
function TChatRooms.GetItem(index: Integer): TChatRoom;
begin
Result:=TChatRoom(inherite
end;
procedure TChatRooms.SetItem(index: Integer; const Value: TChatRoom);
begin
inherited SetItem(index, Value)
end;
{ TChatRoom }
function TChatRoom.AddUser(const userName:string):Boolean;
begin
FUsers.Add(userName)
end;
constructor TChatRoom.Create(Collectio
begin
inherited;
FUsers:=TStringList.Create
FUsers.Sorted:=True
end;
destructor TChatRoom.Destroy;
begin
FUsers.Free;
inherited;
end;
function TChatRoom.GetUserList: TStringList;
begin
Result:=TStringList.Create
Result.Assign(FUsers);
end;
function TChatRoom.RemoveUser(const
var
idx:Integer;
begin
idx:=FUsers.IndexOf(userNa
if (idx>=0) then
FUsers.Delete(idx);
end;
procedure TChatRoom.SetTitle(const Value: string);
begin
FTitle := Value;
end;
ASKER
I tested your code and I got the following error:
111 MyRooms := TChatRooms.Create;
[Error] Unit1.pas(111): Not enough actual parameters.
111 MyRooms := TChatRooms.Create;
[Error] Unit1.pas(111): Not enough actual parameters.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works fine. Thank you
MyRooms : TChatRooms;
MyChatRoom : TChatRoom;
begin
MyRooms := TChatRooms.Create;
MyChatRoom := MyRooms.Add;
MyChatRoom.Title := 'My chat room';
if MyChatRoom.AddUser('My Name') then
showmessage('user added successfully')
else
showmessage(failed to add user');
Memo1.Lines.Assign(MyChatR
if MyChatRoom.RemoveUser('My Name') then
showmessage('user removed successfully')
else
showmessage(failed to remove user');
MyChatRoom.Free;
MyRooms.Free;
end;