Hi,
It sure does help Jeff, thanks a lot ... but you made me more curious
and so I continued testing ... raising new questions
.
I add once more the code at the end in order to make it clear ...
1) I added my interface again. The page
http://msdn.microsoft.com/library/d.../en-us/vbcn7/html/vaconcreatinginterfaces.asp
(step 6) tells me to put it in a module block. I had to move it behind
END MODULE to make it work. Are there any rules about where to place
the interface definition?
2) I implemented the interface in Form2 and Form3 and added method
CallFormLink2 in Form1
Great, it works as you promised. So, I learned interfaces forcing to
implement some code and offering design time intellisense support. Are
there any other reasons to use interfaces? I still think in VB6-ways
I'm afraid
Now, since I wanted to call forms based on there (string) name as done
in CallFormLink, avoiding declarations "as New Form2", "as New Form3"
....
enabling a system where formnames are gathered in a databasetable, a
system where application execution can easily be manipulated via the
database instead of by changing code
....
I wondered I could combine you're interface strategy with the Activator
strategy to call forms "dynamically".
So I introduced CallFormLink3, but here I get a Design time error (*1)
on the line myForm.Show, since the system doesn't early bind myForm with
a form and the interface only supports the method ExecuteLinker.
Than I made a new attempt in CallFormLink4, but with the same result.
So to make it work I need a uninstanciated form object (Dim myForm As
Form ... as in CallFormLink) expanded with the implementation of
IMyInterface. I wonder this can be done, because at this point I also
found out I lost the myForm.Show functionality in CallFormLink2.
I ran the program again and saw "executelinker" being executed in form2
and form3 (the messageboxes appear) but the forms are not shown and I
get a compile time error when adding myForm.Show on the line above
myForm.ExecuteLinker in method CallFormLink2.
So it seems I downgraded the functionality of my forms to only the
method ExecuteLinker by declaring it with type IMyInterface, while I
want to keep the functionality offered by the form class. Any idea?
--
HERE FOLLOWS THE CODE
-- added in module1.vb
Public Interface IMyInterface
Sub ExecuteLinker()
End Interface
-- form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'CallFormLink("Form2")
'CallFormLink2("Form2")
CallFormLink3("Form2")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'CallFormLink("Form3")
'CallFormLink2("Form3")
CallFormLink3("Form3")
End Sub
Private Sub CallFormLink(ByVal strFormName As String)
Dim myForm As Form
Dim ConstructorParameterArray As Object() = New Object() {}
'It seems necessary to specify the name of the application
strFormName = Application.ProductName.ToString & "." & strFormName
myForm =
System.Activator.CreateInstance(Type.GetType(strFormName, True, True),
ConstructorParameterArray)
myForm.Show()
myForm.GetType().InvokeMember("ExecuteLinked",
Reflection.BindingFlags.InvokeMethod, Nothing, myForm, Nothing)
End Sub
Private Sub CallFormLink2(ByVal strFormName As String)
Dim myForm As IMyInterface
Select Case strFormName
Case "Form2"
myForm = New Form2()
Case "Form3"
myForm = New Form3()
End Select
myForm.ExecuteLinker()
End Sub
Private Sub CallFormLink3(ByVal strFormName As String)
Dim myForm As IMyInterface
Dim ConstructorParameterArray As Object() = New Object() {}
'It seems necessary to specify the name of the application
strFormName = Application.ProductName.ToString & "." & strFormName
myForm =
System.Activator.CreateInstance(Type.GetType(strFormName, True, True),
ConstructorParameterArray)
*1 myForm.Show()
myForm.ExecuteLinker()
End Sub
Private Sub CallFormLink4(ByVal strFormName As String)
Dim myForm As IMyInterface
Dim aForm As New Form()
myForm = CType(aForm, IMyInterface)
Dim ConstructorParameterArray As Object() = New Object() {}
'It seems necessary to specify the name of the application
strFormName = Application.ProductName.ToString & "." & strFormName
myForm =
System.Activator.CreateInstance(Type.GetType(strFormName, True, True),
ConstructorParameterArray)
*1 myForm.show()
myForm.ExecuteLinker()
End Sub
-- form2
Implements IMyInterface
Sub ExecuteLinked()
MsgBox("form2")
End Sub
Sub executelinker() Implements IMyInterface.ExecuteLinker
MsgBox("form2 Imp")
End Sub
-- form3
Implements IMyInterface
Sub ExecuteLinked()
MsgBox("form3")
End Sub
Sub executelinker() Implements IMyInterface.ExecuteLinker
MsgBox("form3 Imp")
End Sub
Thanks,
Kind Regards,
Peter Van Wilrijk.