Provide a default implementation of an interface?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
I'm told that vb interfaces cannot have default implementations..

so, how can i ease my workload if i have a situation where i want to make a form in my app searchable, the search routine is always the same (it loops through all the controls on the form, collects values from them, forms an sql and runs it) and i want to offer it on all forms in the app

if i could provide a default implementation in an interface, i could jsut write the interface, and say that the form implements it and bingo, the form has the functionality to be searchable. some aspects would need implementing ona per form basis of course, but the main search sql builder is a fixed logic that i dont want to have to apste into every form

another option is inheriting the form, i.e. i make a SearchableForm that inherits form, then make every form in my app a SearchableForm. I think the IDE will break down in tears if i do that because it seems so tied to its forms that it's an unbreakable


My current woraround that i regard as a bit lame is to have a searchengien class that takes a form as a parameter to a sub. the sub will rip throught he form, build a sql, run it and show the results in another window. upon double click of a certain result, the original form will show some details. i'll probably use a delegate to do this, or make the searchengien a form itself, so it can show its own results, and having direct access to the form to which it relates, can cause a new showing of the results

im wondering if anyone has any other suggestions with this quandary?
 
The concept of Interface indeed do not allow any implementation. I you have a multitiered application where for example the implementation details should not be known at the client application you create an interface which you pass with your client binaries. On the serverside you use the same the interface to build some sort of Base Class of which other classes inherit.

I don't think you're in a multitiered environment, but the solution to your problem is in your question. You should indeed build some sort of SearchableForm base class from which all your other forms inherit from. I don't see the problem in the IDE as long as your base form inherits the System.Windows.Forms.Form class everything should be the same.

kind regards,

Filip
 
I'd love to do this; but I couldnt find a way because of the way forms are treated by the IDE; how would you have me write such a base form?

If i Write

Public Class SearchableForm inherits Form


how to i then use this in the ide? If i make a form, call it SearchableForm then go to any of my existing forms and write "Inherits SearchablForm" in the code view of the form design, it shows an error on SearchableForm, saying "Base Class searcablform specified for frmMyForm cannot be different from the base class Form or one of its other partial types"


To me, this message said "look, windows.forms.form is embedded in the IDE and treated specially; youre going to have so much hassle trying to make a project where all the forms derive from a form you created, that its not worth the effort.."
So i kinda gave up at that point! :)

Does anyone have any experience of providing inheritance with forms?
 
cjard said:
saying "Base Class searcablform specified for frmMyForm cannot be different from the base class Form or one of its other partial types"
In VS2005 the generated designer code is put is a separate partial file attached to the user form code. In the generated code already inheritance is specified, default for a form is inherits system.windows.forms.form. When you in the user partial class try to set another inherits the error above is the result. So, go to design view, click the 'view all files' in solution explorer, click the '+' outside the form you want to change inheritance of, say it's "form2" then open the file "form2.Designer.vb" where the generated code is. At the top of that partial class is the class statement and default inherits statement, where you may very well change system.windows.forms.form to another form type like searchform.
 

Attachments

  • saf.JPG
    saf.JPG
    13.5 KB · Views: 36
It is only the InitializeComponent method that is 'don't touch code' (as described in the generated content you can review in class.Designer.vb file), if you change inheritance to another form the Designer will not change it back to default system form when it 'does its things' later.
 
cool! i've been able to use this to simplify quite a few bits of my project, so once again, john, i thank you for the advice :)
 
Back
Top