|
-
Jul 13th, 2006, 12:49 PM
#1
Thread Starter
New Member
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
-
Jul 13th, 2006, 02:59 PM
#2
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"
-
Jul 13th, 2006, 03:31 PM
#3
Frenzied Member
Re: Passing objects between functions
Why not create the objects in the form?
VB Code:
Private Sub Foo()
Dim objFoo as YourObject
Set objFoo = Something
DoFoo objFoo
End Sub
Private Sub DoFoo(ByVal objFooFoo as YourObject)
'do something with objFooFoo
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
-
Jul 14th, 2006, 06:26 AM
#4
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:
form2.mysub myobj1, myobj2
pete
-
Jul 14th, 2006, 08:20 AM
#5
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:
'FORM CODE:
Private oObj1 As CustomObject
Private oObj2 As CustomObject
Public Property Set ObjectOne(oInput As CustomObject)
Set oObj1 = oInput
End Property
Public Property Set ObjectTwo(oInput As CustomObject)
Set oObj2 = oInput
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:
'SUB CODE:
Private Sub Example()
Dim fForm As UserForm1, oObject1 As CustomObject, oObject2 As CustomObject
'Create objects.
Set oObject1 = New CustomObject
Set oObject2 = New CustomObject
'Create the form.
Set fForm = New UserForm1
'Pass the objects.
Set fForm.ObjectOne = oObject1
Set fForm.ObjectTwo = oObject2
'Show the form.
fForm.Show
Set oObject1 = Nothing
Set oObject2 = Nothing
Unload fForm 'Only if you have the form hiding itself.
Set fForm = Nothing
End Sub
-
Jul 14th, 2006, 09:43 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|