Solved

Chat Room

Posted on 2006-06-26
4
274 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now