Board index » delphi » polymorphism in a procedure header? help

polymorphism in a procedure header? help

hi, I have the following problem:

I have 3 types as shown below

TCase=class(Tobject)
TLetter=class(TCase)
  Title:string;
TNote=class(TCase)

then I have a procedure, which looks something like this:

procedure(Case_to_add: TCase);
var ACase:TLetter;
begin
     if Case_to_add.ClassName='TLetter' then with ACase do
     begin
          Title:=Case_to_add.Title;    
     end;
     ...etc
end;

the compiler tells me that I can not use Title as an identifier as it is not
defined in TCase

Now, Case_to_add can be either a TLetter or a TNote.
how do I define my procedure so I can acces properties that exist in TLetter or
TNote, but not neccesaryly in TCase.

What I really need is a procedure header, where I can get a parameter, which
both can be a TLetter or a TNote.

If anyone knows the answer, please mail me, I will be most gratefull :)

Many thanks in advance
sincearly Jesper Risager
r...@daimi.aau.dk

 

Re:polymorphism in a procedure header? help


Jesper Risager Jensen <r...@daimi.aau.dk> wrote:

Quote
>hi, I have the following problem:
>I have 3 types as shown below
>TCase=class(Tobject)
>TLetter=class(TCase)
>  Title:string;
>TNote=class(TCase)
>then I have a procedure, which looks something like this:
>procedure(Case_to_add: TCase);
>var ACase:TLetter;
>begin
>     if Case_to_add.ClassName='TLetter' then with ACase do
>     begin
>      Title:=Case_to_add.Title;    
>     end;
>     ...etc
>end;
>the compiler tells me that I can not use Title as an identifier as it is not
>defined in TCase
>Now, Case_to_add can be either a TLetter or a TNote.
>how do I define my procedure so I can acces properties that exist in TLetter or
>TNote, but not neccesaryly in TCase.
>What I really need is a procedure header, where I can get a parameter, which
>both can be a TLetter or a TNote.
>If anyone knows the answer, please mail me, I will be most gratefull :)

What you need to do is :

procedure(Case_to_add: TCase);
var ACase:TLetter;
begin
     if (Case_to_add is TLetter) then with ACase do
     begin
          Title:=TLetter(Case_to_add).Title;    
     end;
     ...etc
end;

etc etc.

Also don't forget to Create your ACase instance.

HTH

--
Isaac Hepworth                 ijh1...@cam.ac.uk
Corpus Christi College, Cambridge University, UK

Re:polymorphism in a procedure header? help


Jesper Risager Jensen <r...@daimi.aau.dk> wrote:

Quote
>hi, I have the following problem:
>I have 3 types as shown below
>TCase=class(Tobject)
>TLetter=class(TCase)
>  Title:string;
>TNote=class(TCase)
>then I have a procedure, which looks something like this:
>procedure(Case_to_add: TCase);
>var ACase:TLetter;
>begin
>     if Case_to_add.ClassName='TLetter' then with ACase do
>     begin
>      Title:=Case_to_add.Title;    
>     end;
>     ...etc
>end;
>the compiler tells me that I can not use Title as an identifier as it is not
>defined in TCase
>Now, Case_to_add can be either a TLetter or a TNote.
>how do I define my procedure so I can acces properties that exist in TLetter or
>TNote, but not neccesaryly in TCase.
>What I really need is a procedure header, where I can get a parameter, which
>both can be a TLetter or a TNote.
>If anyone knows the answer, please mail me, I will be most gratefull :)
>Many thanks in advance
>sincearly Jesper Risager
>r...@daimi.aau.dk

You have to typecast the var.  Also, if you want to later decend from
TLetter, your IF statement will not work.  I would suggest the
following code:

procedure(Case_to_add: TCase);
var ACase:TLetter;
begin
     { Add to a TLetter class and all decendants of TLetter }
     if Case_to_add is TLetter then with ACase do
     begin
        Title:=TLetter(Case_to_add).Title;      
     end;
     ...etc
end;

Other Threads