interface as parameter in Class Constructor

The error is “Parameter “view” expects interface IDatabaseExplorerView , but this class is a Window”

I am trying to implement MVP design pattern. A window I am designing inherits an interface. I have a Presenter class that takes the window(“view”) as a parameter as the interface type so the Presenter can call those methods of the interface on the window.

I’m able to do this in C# but how can I implement in XOJO?

Add a new Class to the project. Set its Super to Window. Add your interface to the list of interfaces the new class implements. Change the Super of your window to your new class name.

[quote=388288:@Chad Posner]The error is “Parameter “view” expects interface IDatabaseExplorerView , but this class is a Window”

I am trying to implement MVP design pattern. A window I am designing inherits an interface. I have a Presenter class that takes the window(“view”) as a parameter as the interface type so the Presenter can call those methods of the interface on the window.

I’m able to do this in C# but how can I implement in XOJO?[/quote]

Inherits an interface ? Or implements an interface?

What Tim said will work and another option is to open the inspector for the Window and add the interface to the list of interfaces the Window implements.

This works, I can now pass my class into the constructor, but how do I get the actual Window/GUI back?

“Implements” is what I meant.

@Jason Parsley This method is what is giving me the error when passing it into the constructor. It thinks it’s a window.

Well, now it seems to be working as expected. I just created a new window, implemented my interface(copied and pasted all my controls and code) and now I can pass it into my constructor. I wonder if I can reproduce the error.

I’m not sure what you mean. The purpose of the Interface is so the Presenter class doesn’t have to know that view is a window.

Funny. This should have worked.

I just tried it (on Xojo 2016r2.1, it’s what I had installed on this machine) and it worked. Here’s a simple project that does what Jason described.

https://www.dropbox.com/s/aydhhxksx794h5h/mvc%20test.xojo_binary_project?dl=1

Window1 implements the IDatabaseExplorerView interface directly. Button1 instantiates a new Presenter object and passes it Window1. It compiles without errors.

Hi @Chad Posner

In order to retrieve your Window back… cast it: https://blog.xojo.com/2018/03/21/casting-get-ready-and-keep-the-type/

Anyway, I agree with Tim. In doing that you are losing the advantage of the pattern you’re implementing.

Javier

I’ve updated to from 2017R2 to 2018 R1. Maybe some weird things in 2018R1?

Anyway, this worked for me based on the original post after re-creating everything.

@Tim Hare @Javier Menéndez I wasn’t clear with “getting the window back”:

When doing the following:
Create New Class -> Set It’s Super To “Window” The actual “window” object where I can place controls does not exist.

Going the other way and creating a Window first and then implementing my interface into the window works correctly.

[quote=388330:@Javier Menéndez]Hi @Chad Posner

In order to retrieve your Window back… cast it: https://blog.xojo.com/2018/03/21/casting-get-ready-and-keep-the-type/

Anyway, I agree with Tim. In doing that you are losing the advantage of the pattern you’re implementing.

Javier[/quote]
Thanks for the link. I was actually looking for Casting in the help docs(I didn’t find it) for some other things and I’m glad I don’t need it for this case.

[quote=388411:@Chad Posner]When doing the following:
Create New Class -> Set It’s Super To “Window” The actual “window” object where I can place controls does not exist.[/quote]
That’s the intended behavior. You are creating a subclass of Window, not an instance of it. An instance is the only place you can use the layout editor. You’d have to add controls programmatically otherwise, probably during the open event handler. Which means you’d have to use AddHandler calls for every control you created this way. A lot of work!
(There are exceptions to the rule above like ContainerControls)

So what you did is perfectly fine: Create a Window and set its super to your new subclass, but use the layout editor for the design. If you reuse a certain window design with slight modifications, create a template and copy it for every window instance of this kind.
If you are unsure about the final template design, use a ContainerControl for the window’s general content so you won’t have to redesign every instance you already created.

When you create a class whose Super is Window, it is code-only. You cannot subclass layout. But when you go to a normal window and change its Super to your window subclass, it inherits all the code from that class. That allows you to have a group of windows that all have the same functionality built in.