Passing variable object types to procedures/functions 
Author Message
 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



Fri, 12 Dec 1997 03:00:00 GMT  
 Passing variable object types to procedures/functions
: 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)



Fri, 12 Dec 1997 03:00:00 GMT  
 Passing variable object types to procedures/functions

Quote:

>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



Fri, 12 Dec 1997 03:00:00 GMT  
 Passing variable object types to procedures/functions

Quote:


>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.



Sat, 13 Dec 1997 03:00:00 GMT  
 Passing variable object types to procedures/functions

Quote:

>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;



Sat, 13 Dec 1997 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How to Pass a file type to a function or procedure

2. Passing a pointer to a procedure/function as a parameter in a procedure

3. passing variables to an asm procedure in fpk

4. Easy question: passing variables FROM a procedure

5. Newbie: Passing a Function Variable

6. Passing file variable to Function

7. Passing file variable to Function

8. Passing file variable to Function

9. Passing TForm object in procedures ????

10. Object's and passing Procedure-params

11. Object's and passing Procedure-params

12. Passing Arrays to Procedures and Functions

 

 
Powered by phpBB® Forum Software