The given procedure works fine,thank you very much.
But I have still trouble with the book2Delphi 7, the Bible where this procedure is used
Here the code from bokk,,adapted
unit abstanimf;
{$MODE Delphi}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, ExtCtrls, uplaysound;
type
{ TFormAnimals }
TFormAnimals = class(TForm)
playsound1: Tplaysound;
LabelVoice: TLabel;
BtnVoice: TButton;
RbtnAnimal: TRadioButton;
RbtnDog: TRadioButton;
RbtnCat: TRadioButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BtnVoiceClick(Sender: TObject);
procedure RbtnAnimalClick(Sender: TObject);
procedure RbtnDogClick(Sender: TObject);
procedure RbtnCatClick(Sender: TObject);
procedure PlaySound(AWavFile: String; ASync: Boolean);
private
public
{ Public declarations }
end;
TAnimal = class
public
constructor Create;
function GetKind: string;
function Voice: string; virtual;abstract;
private
Kind: string;
end;
TDog = class (TAnimal)
public
constructor Create;
function Voice: string; override;
function Eat: string; virtual;
end;
TCat = class (TAnimal)
public
constructor Create;
function Voice: string;override;
function Eat: string; virtual;
end;
var
FormAnimals: TFormAnimals;
MyAnimal: TAnimal;
implementation
{$R *.lfm}
procedure TFormAnimals.FormCreate(Sender: TObject);
begin
MyAnimal := TDog.Create;
end;
procedure TFormAnimals.FormDestroy(Sender: TObject);
begin
MyAnimal.Free;
end;
procedure TFormAnimals.BtnVoiceClick(Sender: TObject);
begin
LabelVoice.Caption := MyAnimal.Voice;
end;
constructor TAnimal.Create;
begin
Kind := 'An animal';
end;
function TAnimal.GetKind: string;
begin
GetKind := Kind;
end;
constructor TDog.Create;
begin
Kind := 'A dog';
end;
function TDog.Voice: string;
Var VoiceSound:String;
begin
Voice := 'Arf Arf';
VoiceSound:= 'dog.wav';
FormAnimals.PlaySound(VoiceSound,true);
end;
function TDog.Eat: string;
begin
Eat := 'A bone, please!';
end;
constructor TCat.Create;
begin
Kind := 'A cat';
end;
function TCat.Voice: string;
Var VoiceSound:String;
begin
Voice := 'Mieow' ;
VoiceSound:= 'cat.wav';
FormAnimals.PlaySound(VoiceSound,true);
end;
function TCat.Eat: string;
begin
Eat := 'A mouse, please!';
end;
procedure TFormAnimals.RbtnAnimalClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TAnimal.Create;
end;
procedure TFormAnimals.RbtnDogClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TDog.Create;
end;
procedure TFormAnimals.RbtnCatClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TCat.Create;
end;
procedure TFormAnimals.PlaySound(AWavFile: String; ASync: Boolean);
var
player: TPlaySound;
begin
player := TPlaySound.Create(nil);
try
player.SoundFile := AWavFile;
if Async then player.PlayStyle := psAsync else player.PlayStyle := psSync;
player.Execute;
finally
player.Free;
end;
end;
end.
It compile fine but when I get
unit abstanimf;
{$MODE Delphi}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, ExtCtrls, uplaysound;
type
{ TFormAnimals }
TFormAnimals = class(TForm)
playsound1: Tplaysound;
LabelVoice: TLabel;
BtnVoice: TButton;
RbtnAnimal: TRadioButton;
RbtnDog: TRadioButton;
RbtnCat: TRadioButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BtnVoiceClick(Sender: TObject);
procedure RbtnAnimalClick(Sender: TObject);
procedure RbtnDogClick(Sender: TObject);
procedure RbtnCatClick(Sender: TObject);
procedure PlaySound(AWavFile: String; ASync: Boolean);
private
public
{ Public declarations }
end;
TAnimal = class
public
constructor Create;
function GetKind: string;
function Voice: string; virtual;abstract;
private
Kind: string;
end;
TDog = class (TAnimal)
public
constructor Create;
function Voice: string; override;
function Eat: string; virtual;
end;
TCat = class (TAnimal)
public
constructor Create;
function Voice: string;override;
function Eat: string; virtual;
end;
var
FormAnimals: TFormAnimals;
MyAnimal: TAnimal;
implementation
{$R *.lfm}
procedure TFormAnimals.FormCreate(Sender: TObject);
begin
MyAnimal := TDog.Create;
end;
procedure TFormAnimals.FormDestroy(Sender: TObject);
begin
MyAnimal.Free;
end;
procedure TFormAnimals.BtnVoiceClick(Sender: TObject);
begin
LabelVoice.Caption := MyAnimal.Voice;
end;
constructor TAnimal.Create;
begin
Kind := 'An animal';
end;
function TAnimal.GetKind: string;
begin
GetKind := Kind;
end;
constructor TDog.Create;
begin
Kind := 'A dog';
end;
function TDog.Voice: string;
Var VoiceSound:String;
begin
Voice := 'Arf Arf';
VoiceSound:= 'dog.wav';
FormAnimals.PlaySound(VoiceSound,true);
end;
function TDog.Eat: string;
begin
Eat := 'A bone, please!';
end;
constructor TCat.Create;
begin
Kind := 'A cat';
end;
function TCat.Voice: string;
Var VoiceSound:String;
begin
Voice := 'Mieow' ;
VoiceSound:= 'cat.wav';
FormAnimals.PlaySound(VoiceSound,true);
end;
function TCat.Eat: string;
begin
Eat := 'A mouse, please!';
end;
procedure TFormAnimals.RbtnAnimalClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TAnimal.Create;
end;
procedure TFormAnimals.RbtnDogClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TDog.Create;
end;
procedure TFormAnimals.RbtnCatClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TCat.Create;
end;
procedure TFormAnimals.PlaySound(AWavFile: String; ASync: Boolean);
var
player: TPlaySound;
begin
player := TPlaySound.Create(nil);
try
player.SoundFile := AWavFile;
if Async then player.PlayStyle := psAsync else player.PlayStyle := psSync;
player.Execute;
finally
player.Free;
end;
end;
end.
try to execute it I getunit abstanimf;
{$MODE Delphi}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, ExtCtrls, uplaysound;
type
{ TFormAnimals }
TFormAnimals = class(TForm)
playsound1: Tplaysound;
LabelVoice: TLabel;
BtnVoice: TButton;
RbtnAnimal: TRadioButton;
RbtnDog: TRadioButton;
RbtnCat: TRadioButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BtnVoiceClick(Sender: TObject);
procedure RbtnAnimalClick(Sender: TObject);
procedure RbtnDogClick(Sender: TObject);
procedure RbtnCatClick(Sender: TObject);
procedure PlaySound(AWavFile: String; ASync: Boolean);
private
public
{ Public declarations }
end;
TAnimal = class
public
constructor Create;
function GetKind: string;
function Voice: string; virtual;abstract;
private
Kind: string;
end;
TDog = class (TAnimal)
public
constructor Create;
function Voice: string; override;
function Eat: string; virtual;
end;
TCat = class (TAnimal)
public
constructor Create;
function Voice: string;override;
function Eat: string; virtual;
end;
var
FormAnimals: TFormAnimals;
MyAnimal: TAnimal;
implementation
{$R *.lfm}
procedure TFormAnimals.FormCreate(Sender: TObject);
begin
MyAnimal := TDog.Create;
end;
procedure TFormAnimals.FormDestroy(Sender: TObject);
begin
MyAnimal.Free;
end;
procedure TFormAnimals.BtnVoiceClick(Sender: TObject);
begin
LabelVoice.Caption := MyAnimal.Voice;
end;
constructor TAnimal.Create;
begin
Kind := 'An animal';
end;
function TAnimal.GetKind: string;
begin
GetKind := Kind;
end;
constructor TDog.Create;
begin
Kind := 'A dog';
end;
function TDog.Voice: string;
Var VoiceSound:String;
begin
Voice := 'Arf Arf';
VoiceSound:= 'dog.wav';
FormAnimals.PlaySound(VoiceSound,true);
end;
function TDog.Eat: string;
begin
Eat := 'A bone, please!';
end;
constructor TCat.Create;
begin
Kind := 'A cat';
end;
function TCat.Voice: string;
Var VoiceSound:String;
begin
Voice := 'Mieow' ;
VoiceSound:= 'cat.wav';
FormAnimals.PlaySound(VoiceSound,true);
end;
function TCat.Eat: string;
begin
Eat := 'A mouse, please!';
end;
procedure TFormAnimals.RbtnAnimalClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TAnimal.Create;
end;
procedure TFormAnimals.RbtnDogClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TDog.Create;
end;
procedure TFormAnimals.RbtnCatClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TCat.Create;
end;
procedure TFormAnimals.PlaySound(AWavFile: String; ASync: Boolean);
var
player: TPlaySound;
begin
player := TPlaySound.Create(nil);
try
player.SoundFile := AWavFile;
if Async then player.PlayStyle := psAsync else player.PlayStyle := psSync;
player.Execute;
finally
player.Free;
end;
end;
end.
It compiles with the following warnings
abstanimf.pas(136,29) Warning: Constructing a class "TAnimal" with abstract method "Voice"
abstanimf.pas(41,14) Hint: Found abstract method: Voice(<TAnimal>;<var AnsiString>):AnsiString;
When I execute it I get Class exception Runerror(211)
in line 82 LabelVoice.Caption:=MyAnimalVoice;
When I go on I get
Abstract method called.
Press OK to ignore and risk data corruption.
Press Abort to kill the program.
It is very clear that I lack some understanding regardind the classes
by I have re read the chapter in the book and I do not find my error
The LFM code is
object FormAnimals: TFormAnimals
Left = 733
Height = 233
Top = 170
Width = 278
ActiveControl = BtnVoice
Caption = 'Animals'
ClientHeight = 233
ClientWidth = 278
Color = clBtnFace
Font.CharSet = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
OnCreate = FormCreate
OnDestroy = FormDestroy
LCLVersion = '1.8.0.6'
object LabelVoice: TLabel
Left = 39
Height = 17
Top = 168
Width = 201
Alignment = taCenter
AutoSize = False
Caption = 'Voice'
Font.CharSet = ANSI_CHARSET
Font.Color = clBlack
Font.Height = -16
Font.Name = 'Arial'
Font.Style = [fsBold]
ParentColor = False
ParentFont = False
end
object BtnVoice: TButton
Left = 152
Height = 33
Top = 56
Width = 89
Caption = '&Voice'
OnClick = BtnVoiceClick
TabOrder = 0
end
object RbtnAnimal: TRadioButton
Left = 24
Height = 21
Top = 40
Width = 62
Caption = '&Animal'
OnClick = RbtnAnimalClick
TabOrder = 1
end
object RbtnDog: TRadioButton
Left = 24
Height = 21
Top = 72
Width = 46
Caption = '&Dog'
Checked = True
OnClick = RbtnDogClick
TabOrder = 2
TabStop = True
end
object RbtnCat: TRadioButton
Left = 24
Height = 21
Top = 104
Width = 43
Caption = '&Cat'
OnClick = RbtnCatClick
TabOrder = 3
end
object playsound1: Tplaysound
About.Description.Strings = (
'Plays WAVE sounds in Windows or Linux'
)
About.Title = 'PlaySound'
About.Height = 400
About.Width = 400
About.Font.Color = clNavy
About.Font.Height = -13
About.BackGroundColor = clCream
About.Version = '0.0.7'
About.Authorname = 'Gordon Bamber'
About.Organisation = 'Public Domain'
About.AuthorEmail = 'minesadorada@charcodelvalle.com'
About.ComponentName = 'PlaySound'
About.LicenseType = abModifiedGPL
PlayCommand = 'play'
left = 192
top = 124
end
end
Thanks again.This forum is my lifeboat