Board index » delphi » Passing variable object types to procedures/functions

Passing variable object types to procedures/functions

I am attempting to write a function where one of the parameters, or
arguments, is a variable holding an Object. To be a bit more explicit
I want to pass an object, which may be of type TForm or TPrinter, to
a function which then processes depending on the Object Type.

As an example

Function XYZ(var OutDevice : TObject ; nFontSize : SmallInt);

begin
   case TypeOf(OutDevice)  of
      TPrinter : OutDevice.Canvas.Font.Size := nFontSize;
      TForm    : OutDevice.Canvas.Font.Size := nFontSize - 1;
   end;

end;

This function would then be called similar to :

procedure ButtonClick(........)

begin
   if (some global variable) = 'Printer' then
      XYZ(PrinterObject,15)
   else
      XYZ(FormObject,15);
end;

My problems occur when compiling ( message 'Identifier expected') highlighted
on the first OutDevice line. From here I can't see if the whole idea
is going to work.

Is someone able to advise/steer me in the right direction(s) please.

Maca

 

Re:Passing variable object types to procedures/functions


Quote
Peter McHardie (p-mchar...@adfa.oz.au) wrote:

: begin
:    case TypeOf(OutDevice)  of
:       TPrinter : OutDevice.Canvas.Font.Size := nFontSize;
:       TForm    : OutDevice.Canvas.Font.Size := nFontSize - 1;
:    end;
:    
: end;

: My problems occur when compiling ( message 'Identifier expected') highlighted
: on the first OutDevice line. From here I can't see if the whole idea
: is going to work.

Sure, TObjects don't know an Canvas. You have to typecast the stuff
like (and I couldn't try my idea, no delphi here, so without guarantee):

(Outdevice as TPrinter).Canvas.Font.Size....

or, if that shouldn't work:

var oTPrinter : TPrinter;

begin
        oTPrinter := OutDevice as TPrinter;
        ...

HTH
--
Dipl.Ing. Dr. Peter Lipp - Institute for Applied Information Processing
Technische Universitaet Graz, Austria (University of Technology, Graz, Austria)
pl...@iaik.tu-graz.ac.at

Re:Passing variable object types to procedures/functions


Quote
In article <1995Jun26.054249.4...@sserve.cc.adfa.oz.au> Peter McHardie <p-mchar...@adfa.oz.au> writes:
>I am attempting to write a function where one of the parameters, or
>arguments, is a variable holding an Object. To be a bit more explicit
>I want to pass an object, which may be of type TForm or TPrinter, to
>a function which then processes depending on the Object Type.
>As an example
>Function XYZ(var OutDevice : TObject ; nFontSize : SmallInt);
>begin
>   case TypeOf(OutDevice)  of
>      TPrinter : OutDevice.Canvas.Font.Size := nFontSize;
>      TForm    : OutDevice.Canvas.Font.Size := nFontSize - 1;
>   end;

>end;

You can't do this with Case, but you can do it with If.  For example,

  if OutDevice is TPrinter then
    ...
  else if OutDevice is TForm then
    ...

An advantage of this approach is that the "is" predicate checks for
descendants too, so if someone passes a descendant of TPrinter, your routine
will still work.  Typeof() doesn't do that.

Duncan Murdoch

Re:Passing variable object types to procedures/functions


Quote
In article <1995Jun26.054249.4...@sserve.cc.adfa.oz.au> Peter McHardie <p-mchar...@adfa.oz.au> writes:
>From: Peter McHardie <p-mchar...@adfa.oz.au>
>Subject: [Delphi] Passing variable object types to procedures/functions
>Date: Mon, 26 Jun 1995 05:42:49 GMT
>I am attempting to write a function where one of the parameters, or
>arguments, is a variable holding an Object. To be a bit more explicit
>I want to pass an object, which may be of type TForm or TPrinter, to
>a function which then processes depending on the Object Type.
>As an example
>Function XYZ(var OutDevice : TObject ; nFontSize : SmallInt);
>begin
>   case TypeOf(OutDevice)  of
>      TPrinter : OutDevice.Canvas.Font.Size := nFontSize;
>      TForm    : OutDevice.Canvas.Font.Size := nFontSize - 1;
>   end;

>end;
>This function would then be called similar to :
>procedure ButtonClick(........)
>begin
>   if (some global variable) = 'Printer' then
>      XYZ(PrinterObject,15)
>   else
>      XYZ(FormObject,15);
>end;
>My problems occur when compiling ( message 'Identifier expected') highlighted
>on the first OutDevice line. From here I can't see if the whole idea
>is going to work.
>Is someone able to advise/steer me in the right direction(s) please.
>Maca

A couple of ideas (untested): First, try removing the VAR on the OutDevice.
Getting a reference to it should be enough.
Second, try TypeCasting OutDevice in the CASE statement:

  CASE TypeOf(OutDevice) OF
    TPrinter: TPrinter(Outdevice).Canvas.Font.Size . . .

As I said, this is untested. Not even sure it will compile...

Claude.

Re:Passing variable object types to procedures/functions


Quote
Peter McHardie <p-mchar...@adfa.oz.au> wrote:
>As an example
>Function XYZ(var OutDevice : TObject ; nFontSize : SmallInt);
>begin
>   case TypeOf(OutDevice)  of
>      TPrinter : OutDevice.Canvas.Font.Size := nFontSize;
>      TForm    : OutDevice.Canvas.Font.Size := nFontSize - 1;
>   end;

>end;
>This function would then be called similar to :
>procedure ButtonClick(........)
>begin
>   if (some global variable) = 'Printer' then
>      XYZ(PrinterObject,15)
>   else
>      XYZ(FormObject,15);
>end;
>My problems occur when compiling ( message 'Identifier expected') highlighted
>on the first OutDevice line. From here I can't see if the whole idea
>is going to work.

A case constant has to be a constant of an ordinal type, which a class
pointer isn't.  Also, you need to typecast OutDevice to something
useful.

You might try something like:

Function XYZ(var OutDevice : TObject ; nFontSize : SmallInt);
begin
   if OutDevice is TPrinter then
      TPrinter(OutDevice).Canvas.Font.Size := nFontSize
   else
      TForm(OutDevice).Canvas.Font.Size := nFontSize - 1;
end;

Other Threads