CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2001
    Location
    DC
    Posts
    26

    Handle to an object as a parameter

    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


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Handle to an object as a parameter

    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
    [email protected]

    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
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Handle to an object as a parameter

    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.



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

Featured