Click to See Complete Forum and Search --> : Handle to an object as a parameter


Pascal_c
October 3rd, 2001, 04:24 PM
Hello all,

I would like to know if there's a way to pass a handle to an object as a parameter of another function in VB.
Below is an example:

public Function someFunction
Dim objTest as AnObject
set objTest = new AnObject.Something

' ... some code

' Now I will call another function that needs to have
' access to this object, so I pass it as a parameter
AnotherFunction objTest

' ... some more code

End Function

private Function anotherFunction(handleToObject as Variant)
' I can now use that variable as a handle to the
' object instantiated above to call its properties
handleToObject.someProperty

Exit Function
End Function



So, can we do something like that or I'll have to instantiate the object all over again in each function?
Please any help is greatly appreciated.
Thank you all in advance.

- Pascal

Cakkie
October 4th, 2001, 01:04 AM
The code you gave should work. You just pass along the refference to the object, meaning that you can change properties, call methods, in onther word, the whole stuff.

Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook

DSJ
October 4th, 2001, 08:18 AM
Cakkie is right... your code should work fine as is, but an improvement would be to type your handleToObject "as AnObject" if it's always going to be the same kind of object (AnObject) or to "as Object" if the object type will vary. This will help the compile find errors as well as allow for early binding, etc.