Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2024-05

Type-casting a class variable

11 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Adriaan van Os

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

Olivier Sannier
to Adriaan van Os

Hello,

You should cast to TWindowClass as TWindow is used to cast an instance, not a class reference.

Note that you may not have TWindowClass, in which case you need to declare it like this:

type
     TWindowClass = class of TWindow;

Note that CreateNewWindow would have to be virtual for derived clases methods to be called.

Le 01/05/2024 à 16꞉28, Adriaan van Os via fpc-pascal a écrit :

Show quoted text 25 lines
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  - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Martin Frb
to Adriaan van Os: Suppose I have a var myClass: TClass and (for example) a class function…
Adriaan van Os wrote 13 lines
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.

Sven Barth
to Adriaan van Os: Suppose I have a var myClass: TClass and (for example) a class function…

Adriaan van Os via fpc-pascal schrieb am
Mi., 1. Mai 2024, 17:07:

Show quoted text 14 lines
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

Adriaan van Os
to Sven Barth
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.

But how can myClass be passed ?

myClass.CreateNewWindow

is not accepted by the compiler, because CreateNewWindow is not a method of TClass, whereas

TWindow.CreateNewWindow

doesn't pass myClass.

Regards,

Adriaan van Os

Martin Frb
to Adriaan van Os
Adriaan van Os wrote 12 lines
Martin Frb via fpc-pascal wrote: > Silly question, but did you assign the value to the variable? > > myClass := TWindow; That's what I did. > TWindow(myClass).CreateNewWindow; And this is what crashes. I can report this, if the type-cast is supposed to work.

My example (2nd part of it) was actually wrong.

It was mentioned before

Jean SUZINEAU wrote 7 lines
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( ....);

It must be cast to another "class of"

Adriaan van Os
to Martin Frb
Martin Frb wrote 14 lines
My example (2nd part of it) was actually wrong. It was mentioned before On 01/05/2024 19:43, Jean SUZINEAU via fpc-pascal wrote: > 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( ....); It must be cast to another "class of"

When I do that, the compiler emits

Class or COM interface type expected, but got "TClass"

I am using

	{$mode macpas}
	{$modeswitch class}
	{$interfaces corba}

Regards,

Adriaan van Os

Marco van de Voort
to Adriaan van Os: That's what I did. And this is what crashes. I can report this, if the type-cast is…

Op 2-5-2024 om 08:32 schreef Adriaan van Os via fpc-pascal:

Show quoted text 6 lines
> TWindow(myClass).CreateNewWindow; And this is what crashes. I can report this, if the type-cast is supposed to work.

Known gotcha. Is and as are no good for method variables, use:

if myclass.inheritsfrom(twindow) then

    twindow(myclass).createnewwindow

else

   raise exception.create('something''s rotten in the state of ...');