Results 1 to 7 of 7

Thread: simple code problem....?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Location
    nashville, tn
    Posts
    11

    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

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'inside form1 but not in any event
    2. Dim family As New Collection()
    3.  
    4. Public Sub Form1_Load....
    5.    'nothing needed here
    6. End Sub
    7.  
    8. Private Sub Button1_Click...
    9.    family.Add("Tom")
    10. End Sub

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    No because its scope is still specific to that sub.

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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
  •  



Click Here to Expand Forum to Full Width