ByVal object parameters not modified over Remoting

J

JB

Hi All,

I've discovered a strange behaviour with Object parameters passed
ByVal via remoting and I'm wondering if anybody could shed some light
on this.

In a non remoting function call, when a object (as opposed to a value
type like Integer, Boolean, etc) is passed as a ByVal parameter, it's
content can be modified. This is somehow "strange", but I've lived
with that so far.
Now when the exact same function is called via remoting (i.e. the
parameter will be serialized for the call) the content of the object
is NOT modified. It looks a bit inconsistent to me and I'd like to
know if I'm missing something. Or is there a way I can change this
without having to use ByRef.
Code sample below.

Thanks
JB

<Serializable()> _
Class CTest
Public Value As String
End Class

Public Sub ChangeValueByVal(ByVal Test As CTest)
Test.Value = "Value Has been changed by the Sub (ByVal)"
End Sub

Public Sub ChangeValueByRef(ByRef Test As CTest)
Test.Value = "Value Has been changed by the Sub (ByRef)"
End Sub

Dim Test As New CTest

'In Non Remoting Environement (i.e. ChangeValueByVal and
ChangeValueByRef called locally)
'====================================================================
Test.Value = "Initial Value"

ChangeValueByVal(Test)
'Upon exit Test contains "Value Has been changed by the Sub (ByVal)"
'<== OK

ChangeValueByRef(Test)
'Upon exit Test contains "Value Has been changed by the Sub (ByRef)"
'<== OK

'So Far so Good...
'Now In a Remoting Environement (i.e. ChangeValueByVal and
ChangeValueByRef called remotely)
'=======================================================================
Test.Value = "Initial Value"

ChangeValueByVal(Test)
'Upon exit Test contains "Initial Value" '<== Inconsistent, why is
that !!!!

ChangeValueByRef(Test)
'Upon exit Test contains "Value Has been changed by the Sub (ByRef)"
'<== OK
 
G

Guest

Yes, I suppose you could call it inconsistant, but it is expected. Passing a
reference type byval, in process, simply sends the reference, allowing the
routine to modify the objcet referenced. Whereas, if sent byref, the routine
could also modify the reference itself. I am no expert on remoting, but my
understanding is that the referenced object will be 'copied' to the remote
process and since it is byval, no provision to copy it back would be made.
 
M

Mattias Sjögren

In a non remoting function call, when a object (as opposed to a value
type like Integer, Boolean, etc) is passed as a ByVal parameter, it's
content can be modified. This is somehow "strange", but I've lived
with that so far.
Now when the exact same function is called via remoting (i.e. the
parameter will be serialized for the call) the content of the object
is NOT modified. It looks a bit inconsistent to me and I'd like to
know if I'm missing something. Or is there a way I can change this
without having to use ByRef.

If you derive your class from MarshalByRefObject it will give you the
result you expected. Classes that don't derive from MBRO are only
marshaled one way in the remoting call for efficiency reasons.


Mattias
 
J

JB

If you derive your class from MarshalByRefObject it will give you the
result you expected. Classes that don't derive from MBRO are only
marshaled one way in the remoting call for efficiency reasons.

Mattias

Hi Mattias,

Thanks very much for that. It makes sense now. I did read a lot about
remoting but never read that anywhere. Much appreciated.

JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top