Re: [fpc-pascal] Type-casting a class variable

2024-05-01 Thread Sven Barth via fpc-pascal
Adriaan van Os via fpc-pascal  schrieb am
Mi., 1. Mai 2024, 17:07:

> Suppose I have a
>
> var myClass: TClass
>
> and (for example) a
>
> class function TWindow.CreateNewWindow( )
>
> Now I want to call CreateNewWindow for var myClass. Of course, depending
> on the class,
> CreateNewWindow will behave different. Type-casting myClass to TWindow
> crashes (in my setup). And
> even if it worked, wouldn't it loose the class information ?
>

In addition to what Martin said: as long as you have a non-static class
method the value of the variable you call the class method on (e.g. myClass
in your example) will be passed as Self parameter. So no need for extra
parameters.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Type-casting a class variable

2024-05-01 Thread Martin Frb via fpc-pascal

On 01/05/2024 16:28, Adriaan van Os via fpc-pascal wrote:

Suppose I have a

var myClass: TClass

and (for example) a

class function TWindow.CreateNewWindow( )

Now I want to call CreateNewWindow for var myClass. Of course, 
depending on the class, CreateNewWindow will behave different. 
Type-casting myClass to TWindow crashes (in my setup). And even if it 
worked, wouldn't it loose the class information ?




Silly question, but did you assign the value to the variable?

myClass := TWindow;
TWindow(myClass).CreateNewWindow;

The cast is not always needed.

If you have

var myClass: TMyClass;

with
  TMyClass = class
   class  procedure  CreateNewWindow; virtual
  end;

And any inherited class correctly overrides this, then you don't need 
the typecast.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Type-casting a class variable

2024-05-01 Thread Jean SUZINEAU via fpc-pascal

I didn't tested but I imagine it could be done with something like this ?

type
   TWindow_Class= class of TWindow;
begin
  ...
  (myClass as TWindow_Class).CreateNewWindow( );

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Type-casting a class variable

2024-05-01 Thread Adriaan van Os via fpc-pascal

Suppose I have a

var myClass: TClass

and (for example) a

class function TWindow.CreateNewWindow( )

Now I want to call CreateNewWindow for var myClass. Of course, depending on the class, 
CreateNewWindow will behave different. Type-casting myClass to TWindow crashes (in my setup). And 
even if it worked, wouldn't it loose the class information ?


Of course, by adding an extra parameter of type TClass to CreateNewWindow, the issue can be solved. 
But that's far from elegant.


How to solve this ? Anything I missed ?

Regards,

Adriaan van Os

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal