runtime binding

vishalmarya

New member
Joined
Aug 6, 2006
Messages
4
Location
New Delhi, India
Programming Experience
5-10
i have a form ( form2) , which can be called by any other form. all other forms have a sub test. depending on which form has called form2 , sub test is executed ( the procedure of the calling form ).

the code below works fine in vb6 , but equivalent vb.net code got problems.

==============
Vb 6 code :

form1 :

Private Sub Button1_Click

Dim ofrm As New Form2
set ofrm.senderfrm = Me
ofrm.Show()

end sub

public sub test()
msgbox "this is test"
end sub

-----------------------------

form2

''' declaration section
Public senderfrm As form

Private Sub Button1_Click
senderfrm.test()
end sub


=======================

vb.net code

form1 :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ofrm As New Form2
ofrm.senderfrm = Me
ofrm.Show()

End Sub

Public Sub test()
MsgBox("this is test")
End Sub

-----------------

form 2 :

''' declaration section
Public senderfrm As System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

senderfrm.test()
' ERROR " test not a member "

End Sub
 
or dim senderfrm as Form1, rather than a generic form.

-tg
 
Solution1:
You can check the type of the generic form object with the TypeOf operator and cast it accordingly to the appropriate type, which can invoke its type specific 'test' member. Example where 'caller' is declared as Form:
VB.NET:
If TypeOf caller Is Form2 Then
  DirectCast(caller, Form2).test()
ElseIf TypeOf caller Is Form3 Then
  DirectCast(caller, Form3).test()
End If

Solution2:
This is also typically a situation where a Interface is useful. You can declare this simple interface outside any classes/forms, for example add a new class to the project and let this code be the only text in that file:
VB.NET:
Public [COLOR=darkgreen]Interface Iform[/COLOR]
  Sub test()
End Interface
The form that is called by the different others may contain this code, see the 'caller' type here is the interface type (and not generic Form or specific Form2,FormC):
VB.NET:
Public Class Form1
 
Public caller As [COLOR=darkgreen]Iform[/COLOR]
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  [COLOR=darkgreen]caller.test()[/COLOR]
End Sub
 
End Class
Notice it calls the interface method 'test', and not specifically a method of a specific form.

Each other form that call this must then implement the interface, example:
VB.NET:
Public Class Form2
[COLOR=darkgreen]Implements Iform[/COLOR]
 
Public Sub form2test() [COLOR=darkgreen]Implements Iform.test[/COLOR]
  MsgBox("hello from " & Me.Name)
End Sub
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim f As New Form1
  f.caller = Me
  f.Show()
End Sub
 
End Class
For this example set, when the interface method 'test' is called on form2 object it is the 'form2test' sub method that is invoked.
 
Back
Top