DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Passing a collection back to client ByVal?

  1. #1
    Guest

    Passing a collection back to client ByVal?

    Hello all,

    I have an an out of process server that keeps a collection of clients. In
    the registration of a new client I would like to pass back a collection of
    objects. I can get that done, however the collection is ByRef and fails
    the moment I try to use it with a runtime error 98.

    My question, is there any way to get this collection back to the client by
    value.

    I considered passing back a recordset but then the client becomes laden with
    the MDAC components. I would like to keep it light.

    Thanks in advance

    Greg Davis




  2. #2
    Dan Nuttle Guest

    Re: Passing a collection back to client ByVal?


    Well, unless someone else proves me wrong, the answer is not a happy one for
    you. Objects are always...ALWAYS...passed by reference. Even if you use
    ByVal. If you want a copy of an object, you have to do it yourself. That
    would involve creating a new collection object, then making a copy of each
    object inside the original collection, and putting that copy into the new
    collection. VB doesn't have built-in cloning like Java.

    And if you've considered passing an array of objects instead of a collection,
    then you're still out of luck because arrays can also only be passed ByRef.
    Using ByVal generates an error.

  3. #3
    Joel Ryan Guest

    Re: Passing a collection back to client ByVal?

    I believe you are correct.

    A slight clarification: You can pass objects ByVal. However, it doesn't copy
    the object, it makes a new reference to the object. Try the following code:

    Class Class1

    Public sText as String

    ....

    In a form:
    Private Sub Form_Load()
    Dim oThing1 As Class1
    Set oThing1 = New Class1
    oThing1.sText = "Hello World!"

    ' This will work
    ObjectByVal oThing1
    MsgBox oThing1.sText

    ' This will cause an error
    ObjectByRef oThing1
    MsgBox oThing1.sText
    End Sub

    Function ObjectByRef(ByRef oClass As Class1)
    Set oClass = Nothing
    End Function

    Function ObjectByVal(ByVal oClass As Class1)
    Set oClass = Nothing
    End Function


    Dan Nuttle wrote:

    > Well, unless someone else proves me wrong, the answer is not a happy one for
    > you. Objects are always...ALWAYS...passed by reference. Even if you use
    > ByVal. If you want a copy of an object, you have to do it yourself. That
    > would involve creating a new collection object, then making a copy of each
    > object inside the original collection, and putting that copy into the new
    > collection. VB doesn't have built-in cloning like Java.
    >
    > And if you've considered passing an array of objects instead of a collection,
    > then you're still out of luck because arrays can also only be passed ByRef.
    > Using ByVal generates an error.



  4. #4
    Guest

    Re: Passing a collection back to client ByVal?

    Yep...That was my experience. If I remove a member of the collection and
    get the count in the client afterwards the count is decremented.

    It lets you get the reference back but you aren't allowed to do anything
    worthwhile, I suppose the marshalling overhead wouldn't make it worthwhile
    in any case.

    I have worked aroung this by passing a stream back to the client.

    Thank you both for your help,

    Greg


    "Joel Ryan" <jryan@vsi-hq.com> wrote in message
    news:399AD732.16180B86@vsi-hq.com...
    > I believe you are correct.
    >
    > A slight clarification: You can pass objects ByVal. However, it doesn't

    copy
    > the object, it makes a new reference to the object. Try the following

    code:
    >
    > Class Class1
    >





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center