Can a class use an interface with a property of itself?

I’d like to create an interface or superclass with properties that the main window and many child windows share. One of those properties is a Window property typed as the main window. My question is, can the main window use that interface or superclass if it has a property that’s declared as itself (the main window)?

Most of my child windows have the main window as their parent, but some do not. What’s the proper way to override this without shadowing?

Maybe there’s a better way to do this?

Thank you

You lost me there. Can you explain what you mean a little better?

If you want to create common logic/methods/properties that all windows inherit, then you would create a superclass. Add a Class to the project (not a Window) and set its Super to “Window”. Add properties and methods to this class. Then when you create a new Window in the project, change its Super from Window to your new class.

If you want to create a set of common method names that each window implements, but each one implements slightly differently, then you would create a Class Interface and assign that Interface to each window.

I have one main window with several listboxes on it, grouping and summarizing different data. A button next to each listbox opens a “child” window to allow the user to edit the data summarized in the listbox. When the user clicks save on the child window, I call ParentWindow.SomeMethodOnTheMainWin() to update the listbox on the main window.

I was trying to organize (create a superclass) so that I didn’t have to give each child window a ParentWindow property. Also many child windows (and the main window) use the same property names.

So I created a superclass called MyWindow and added a property (among others) called ParentWindow as winMain (my main window). I want my child windows to use this superclass, but I also want the main window to use this superclass. I just don’t know if I could use this superclass for winMain since the superclass contains a property declared as a winMain.

So you really want both. A superclass to contain the common properties and helper methods and an Interface to define the methods that the child windows call on the parent. Change ParentWindow to

ParentWindow as MyWindowInterface

Have winMain and any other window you may want to use the child windows on implement the interface.

Note that this is for better encapsulation. There is no reason you couldn’t have winMain have a property declared as winMain. It just isn’t as extensible.

Thanks Tim. I think I understand. Can you tell me how I should write the child window’s Constructor? Right now I have:

Sub Constructor(w as winMain) self.ParentWindow = w End Sub

But when trying to access a ParentWindow property, the compiler tells me, “This item does not exist”.

Sub Constructor(w as MyWindowInterface)

w should be the same type as the declaration of ParentWindow.

But MyWindowInterface knows nothing about the properties either. The common properties are defined in the superclass.

The child window shouldn’t really be manipulating the parent window’s properties directly. It should go through the API you’ve defined in the Interface and pass data to methods, which are then free to do whatever they please with it, without the child window needing to know the details. That’s encapsulation.

If you don’t want to go down that path, then go back to ParentWindow as winMain. There’s nothing technically wrong with that. winMain can have the property, too, no problem (it just won’t do anything with it).

They don’t manipulate any properties, they just need to know what they are. For example, the main window creates a record in the database and stores the row id in a property of the main window. The child window needs to know what that id is so that it can save its related data to the database.

Same thing. Either add a function to your Interface that returns the ID or use winMain directly.

How do I expose the superclass’s helper methods to the child windows if I’m passing w as the interface?

Sub Constructor(w as MyWindowInterface) self.ParentWindow = w End Sub

Should I have MyWindowSuperClass implement MyWindowInterface and then pass the w as MyWindowSuperclass?

Sub Constructor(w as MyWindowSuperclass) self.ParentWindow = w End Sub

And in the Superclass change the ParentWindow property to ParentWindow as MyWindowSuperclass?


Add the helper methods to the interface.