Link to home
Start Free TrialLog in
Avatar of overlook2004
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]:TChatRoom read GetItem write SetItem;
  end;

implementation

{ TChatRooms }

function TChatRooms.Add: TChatRoom;
begin
  Result:=TChatRoom(inherited Add())
end;

function TChatRooms.GetItem(index: Integer): TChatRoom;
begin
  Result:=TChatRoom(inherited 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(Collection: 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(userName);
  if (idx>=0) then
    FUsers.Delete(idx);
end;

procedure TChatRoom.SetTitle(const Value: string);
begin
  FTitle := Value;
end;

Avatar of atul_parmar
atul_parmar
Flag of India image

var
  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(MyChatRoom.GetUserList); //
  if MyChatRoom.RemoveUser('My Name') then
    showmessage('user removed successfully')
  else
    showmessage(failed to remove user');
  MyChatRoom.Free;
  MyRooms.Free;
end;
Avatar of overlook2004
overlook2004

ASKER

I tested your code and I got the following error:

111         MyRooms := TChatRooms.Create;

[Error] Unit1.pas(111): Not enough actual parameters.
ASKER CERTIFIED SOLUTION
Avatar of atul_parmar
atul_parmar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works fine.  Thank you