Drizzt Posted August 25, 2003 Posted August 25, 2003 Hi all, In my startup form called formMain I create a new instance of FormEdit. When I close this form I want to call a function in formMain. When I try formMain.functionname () I'm getting the error: Reference to a non-shared member requires an object reference..... Can somebody help me with this? ThanX Grtz Drizzt Quote
*Experts* Volte Posted August 25, 2003 *Experts* Posted August 25, 2003 You need to use your instance of the form, not the form class itself.Dim f As New FormMain f.Method() '<-- Valid FormMain.Method() '<-- Invalid if the Method is not declared "Shared" Quote
Drizzt Posted August 25, 2003 Author Posted August 25, 2003 Thanx for your reply. But since formMain is the startup form, I guess an instance of FormMain is automatically created when the application starts.... I also tried to make the function shared. But then I have to replace all the Me. statements in my function for an expliciet instance name. Is there no other way? Quote
wyrd Posted August 25, 2003 Posted August 25, 2003 So you're calling your main form from another form? You'll have to pass an instance of FormMain to FormEdit (using the constructor would be best). Quote Gamer extraordinaire. Programmer wannabe.
Drizzt Posted August 25, 2003 Author Posted August 25, 2003 Yes that's correct. I'm new to VB so can you please show me how to do this? Quote
*Experts* mutant Posted August 25, 2003 *Experts* Posted August 25, 2003 YOu would have to edit the constructor of your second form: 'a variable to store the instance Dim mainform As FormMain Public Sub New(ByVal form1 As FormMain) 'accept a form instance MyBase.New() InitializeComponents() mainform = form1 'set the passed in instance to the local variable Then in your second form you can use the mainform variable to access the instance that was passed in. And to declare your second form you need to do this: Dim editform As New FormEdit(Me) 'using Me becasuse Im assuming you call this from your FormMain Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.