Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

runtime binding

Status
Not open for further replies.

vishalmarya

Programmer
Aug 19, 2001
28

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
 
You are getting this error because 'senderfrm' is declared as type System.Windows.Forms.Form, and this type does not have a method named 'test'. If you want to reference the 'test' method, you need to declare 'senderfrm' as the type of the calling form. So if the calling form is Form1, the declaration would be:

Dim senderfrm As [red]Form1[/red]

However, this still does not quite do what you want, because you want this to be more 'generic'. That is, you want to be able to assign any form to 'senderfrm', and not have to declare a variable for each type of form that may call Form2. To do this, declare 'senderfrm' as type Object:

Dim senderfrm As Object

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top