|
-
Sep 30th, 2003, 10:40 PM
#1
Thread Starter
New Member
simple code problem....?
Any idea why my button1_click event handler can't access the collection I created in the form_load event? I don't know what I'm doing wrong...
Code:
Public Sub Form1_Load....
Dim family As New Collection()
End Sub
Private Sub Button1_Click...
family.Add("Tom") ' Error: name "family" not declared!!
End Sub
-
Sep 30th, 2003, 11:00 PM
#2
Frenzied Member
Are you sure form load event is PUBLIC not PRIVATE?
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Sep 30th, 2003, 11:35 PM
#3
You are declaring the family variable inside the Form_load event/sub which means it is not valid outside of that sub. So any reference to it that is after the End Sub will fail. You need to declare it in the form declarations not in an event for it to be in the right scope.
VB Code:
'inside form1 but not in any event
Dim family As New Collection()
Public Sub Form1_Load....
'nothing needed here
End Sub
Private Sub Button1_Click...
family.Add("Tom")
End Sub
-
Sep 30th, 2003, 11:37 PM
#4
Frenzied Member
Originally posted by Edneeis
You are declaring the family variable inside the Form_load event/sub which means it is not valid outside of that sub.
What if the SUB is Public? still not available outside it?
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Sep 30th, 2003, 11:38 PM
#5
No because its scope is still specific to that sub.
-
Sep 30th, 2003, 11:38 PM
#6
Frenzied Member
It seems i am confusing classes and subs.
Edit:Ok, got it! great confusion of me!
Last edited by Lunatic3; Sep 30th, 2003 at 11:44 PM.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Sep 30th, 2003, 11:43 PM
#7
Most likely because a form is just a class. So if it is public in the class then its scope extends to anywhere in the class for that particular instance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|