Solved

Chat Room

Posted on 2006-06-26
4
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 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