Results 1 to 6 of 6

Thread: Passing objects between functions

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    13

    Passing objects between functions

    I have a function that creates two objects and shows a form...

    Other than making the objects global, is there a way to pass them to the form and its event-driven functions? I'm just using Form.Show to display the form and the event-driven form functions no longer have the objects in scope by the time they run...

    I hope my question makes sense to someone here, I'm new to object-oriented programming. I'm familiar with using pointers to pass arrays of data in C but have not used pointers with VBA, not to mention I don't see how even pointers would help me out here...

    Any advice would be appreciated. Thanks,

    Dave

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Passing objects between functions

    could you explain a little more?

    what are you creating as far as objects go?
    are the on the current form?

    when you create a new form.. it will have the objects on it that it started with (I think) you might need to re-create on the new form?

    what functions do you want these objects to be effected in?

    what app are you doing this in?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Passing objects between functions

    Why not create the objects in the form?
    VB Code:
    1. Private Sub Foo()
    2.    Dim objFoo as YourObject
    3.    Set objFoo = Something
    4.    DoFoo objFoo
    5. End Sub
    6.  
    7. Private Sub DoFoo(ByVal objFooFoo as YourObject)
    8.    'do something with objFooFoo
    9. End Sub
    You could Dim objFoo in the General Declarations section to make it a form, or module level variable, accessible to event procedures in the form, Setting it in the Form Load event, or just pass it like above, if that works for you.
    Or am I misunderstanding you?
    Tengo mas preguntas que contestas

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Passing objects between functions

    you should be able to pass the objects to the a public sub or function on the form

    VB Code:
    1. form2.mysub myobj1, myobj2

    pete

  5. #5
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Passing objects between functions

    What I usually do is make a custom property on the form that can be set with the objects that need to be passed to it.
    VB Code:
    1. 'FORM CODE:
    2. Private oObj1 As CustomObject
    3. Private oObj2 As CustomObject
    4.  
    5. Public Property Set ObjectOne(oInput As CustomObject)
    6.  
    7.     Set oObj1 = oInput
    8.  
    9. End Property
    10.  
    11. Public Property Set ObjectTwo(oInput As CustomObject)
    12.  
    13.     Set oObj2 = oInput
    14.  
    15. End Property
    Then, you treat the form like any other object in your code, setting properties, getting return values, etc. I personally use this method to handle all of my forms.
    VB Code:
    1. 'SUB CODE:
    2. Private Sub Example()
    3.  
    4.     Dim fForm As UserForm1, oObject1 As CustomObject, oObject2 As CustomObject
    5.    
    6.     'Create objects.
    7.     Set oObject1 = New CustomObject
    8.     Set oObject2 = New CustomObject
    9.    
    10.     'Create the form.
    11.     Set fForm = New UserForm1
    12.     'Pass the objects.
    13.     Set fForm.ObjectOne = oObject1
    14.     Set fForm.ObjectTwo = oObject2
    15.     'Show the form.
    16.     fForm.Show
    17.    
    18.     Set oObject1 = Nothing
    19.     Set oObject2 = Nothing
    20.    
    21.     Unload fForm            'Only if you have the form hiding itself.
    22.     Set fForm = Nothing
    23.    
    24. End Sub

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Passing objects between functions

    I agree 100% with Comintern's approach.
    For more on this methodology see this post
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

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