executing form.sub from a module

  • Thread starter Thread starter excelleinc.com
  • Start date Start date
E

excelleinc.com

Hello,

I'm trying to put sub that's shared between all forms in my app in module so
when I make change I just change it one time.

This sub should execute and then invoke sub defaults() that's different for
each form and is contained within the forms. You'll probably ask why don't I
just put it in the form after the call to module.function, well it's a
handler for one combobox so technically I don't call it.

I made a reference to mainform form and then did
Module sharedFunction
Friend Sub cbo_KeyUp(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyEventArgs)
'do something
frmMainForm.ActiveMdiChild.Defaults()
End Function
End Module

but I'm getting error message in Visual Studio .Net "C:\Visual
Studio\blabla\functions.vb(267): 'Defaults' is not a member of
'System.Windows.Forms.Form'."
the program executes ok and it does call sub properly but every time I want
to recompile I'm getting build error warning.

btw... sub name (Defaults) is same on each of the forms.


Thanks in advance ...
 
Create a MyDefaults Interface with a Sub Defaults

Add the line Implements MyDefaults to each form's class

Then in your shared cbo_KeyUp sub, use CType(frmMainForm.ActiveMdiChild,
MyDefaults).Defaults()

e.g.

Public Class Form1
Inherits System.Windows.Forms.Form
Implements MyDefaults

Public Sub Defaults() Implements MyDefaults.Defaults
'your custom form specific code here
End Sub
End Class

Public Interface MyDefaults
Sub Defaults()
End Interface

Module sharedFunction
Public frmMainForm As FormMain
Friend Sub cbo_KeyUp(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs)
'do something
CType(frmMainForm.ActiveMdiChild, MyDefaults).Defaults()
End Sub
End Module
 
This is not going to help because I can't call this from module:

CType(frmMainForm.ActiveMdiChild, MyDefaults)

Just like I said, I have different forms calling this sub and in this case
MyDefaults will be bunch of different forms.

Even if I don't involve interfaces I'm able to call
CType(frmMainForm.ActiveMdiChild, MyDefaults).Defaults() from the Module but
as I explained, I'm not able to CType to specific form.
 
You are not casting to a specifc form, you are casting each form that
implements the interface to an object that implements that interface. You
could even test each form to see see if it implements that interface using
If frmMainForm.ActiveMdiChild Is TypeOf MyDefault Then.

All of the forms will implement that interface and therefore have a Defaults
sub.

MyDefaults is the name of the Interface you define, I just used that one as
an example.

If you don't believe me, do a proof of concept test of this pattern.
 
You are correct and yes, it does work :)

Had problems yesterday as I put definition for interface within the class so
it was giving me error.


Thank you very much ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top