Are Class parameters allowed?

I want to create a procedure that creates a modal form of a given class.

I create the following declaration.

[async] procedure ShowModalClassForm(AForm :class of TscCustomWebForm);

After in the implementation section, I create the code:

procedure TMainForm.ShowModalClassForm(AForm :class of TscCustomWebForm);
var ModalForm  :TscCustomWebForm;
    Modal      :TModalResult;
begin
...

And I get the next error in the implementation:

[Error] Main.pas(331): identifier not found "TMainForm.ShowModalClassForm"

Can the transpiler create this kind of parameter? Or is there another problem that I have?

It seems that Javascript naturally allows the passing of classes as a function parameter, but the transpiler is not able to transform the Delphi style to Javascript.

I've discovered a workaround to this limitation.

I've created a "type of class type".
It sounds complicated, but the essence is that they work.

type 
   TscForm = class of TscCustomWebForm;

    TMainForm = class(TWebForm)
    ...
       [async] procedure ShowModalForm(AFormClass :TscForm);
    ....
    end;


implementation

procedure TMainForm.ShowModalForm(AFormClass :TscForm);
var ModalForm :TscCustomWebForm;
    Modal     :TModalResult;
begin
   ModalForm := AFormClass.Create(Self);
   ModalForm.Border   := fbNone;
   ModalForm.Popup    := True;
   ModalForm.WebSetup := FWebSetup;

   await(TscCustomWebForm, ModalForm.Load());
   TMisc.BlurStatus(True);
   try
      Modal := await(TModalResult, ModalForm.Execute);
      if Modal = mrOK then begin
      end;
   finally
      TMisc.BlurStatus(False);
      ModalForm.Free;
   end;
end;

Now, I can call this method in this form:

ShowModalForm(TCallUpsForm);

Note that TCallUpsForm is a class. Not an instance of a class. 

 TCallUpsForm descent of TscCustomWebForm, of course. 

I have a method able to instantiate any descendant of TscCustomWebForm.

Seems like the pas2js transpiler has an issue with the way this parameter is specified. We'll bring this up with the pas2js team.

A solution:

type
  TFormclass = class of TCustomForm;
  TForm1 = class(TWebForm)
    WebButton1: TWebButton;
    procedure WebButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure ShowFromClass(aclass: TFormClass);

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ShowFromClass(aclass: TFormClass);
begin
  console.log('aclass param', aclass);
end;

procedure TForm1.WebButton1Click(Sender: TObject);
begin
  ShowFromClass(TForm1);
end;

What curious!

As you can see, I've arrived at the same solution, with a difference of seconds.

In any case, thank you.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.