1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
{$M+}
TJoueur = class(TObject)
private
FPlayerName: string;
procedure SetPlayerName(const Value: string);
public
class function IsValidName(List: TStrings; NewName: string): boolean;
constructor Create(NewName: string);
published
property PlayerName: string read FPlayerName write SetPlayerName;
end;
var
Form1: TForm1;
List: TStrings;
implementation
uses
typinfo;
{$R *.DFM}
{ TJoueur }
constructor TJoueur.Create(NewName: string);
begin
FPlayerName := NewName;
end;
class function TJoueur.IsValidName(List: TStrings; NewName: string): boolean;
begin
// tester if assigned(list) then
Result := List.IndexOf(NewName) < 0;
if Result then List.Add(NewName) else
MessageDlg(Format('%s existe déjà dans un autre objet', [NewName]),
mtWarning, [mbOk], 0);
end;
procedure TJoueur.SetPlayerName(const Value: string);
begin
FPlayerName := Value;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
J, K, L: TJoueur;
begin
if TJoueur.IsValidName(List, 'TOTO') then
J := TJoueur.Create('TOTO');
if TJoueur.IsValidName(List, 'RIRI') then
K := TJoueur.Create('RIRI');
if TJoueur.IsValidName(List, 'RIRI') then
L := TJoueur.Create('RIRI');
ListBox1.Items.Add(J.PlayerName);
ListBox1.Items.Add(K.PlayerName);
FreeAndNil(J);
FreeAndNil(K);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
List := TStringList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// tester if assigned(list) then
FreeAndNil(List);
end;
end. |
Partager