Solved

Chat Room

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

12 Experts available now in Live!

Get 1:1 Help Now