Solved

Chat Room

Posted on 2006-06-26
4
283 Views
Last Modified: 2010-04-05
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;

0
Comment
Question by:overlook2004
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16984356
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;
0
 

Author Comment

by:overlook2004
ID: 16984616
I tested your code and I got the following error:

111         MyRooms := TChatRooms.Create;

[Error] Unit1.pas(111): Not enough actual parameters.
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 250 total points
ID: 16984745
Sorry, that was not tested. check the following now

var
  MyRooms : TChatRooms;
  MyChatRoom : TChatRoom;
begin
  MyRooms := TChatRooms.Create(TChatRoom);
  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;
0
 

Author Comment

by:overlook2004
ID: 16984845
It works fine.  Thank you
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question