473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ByVal object parameters not modified over Remoting

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

Sep 14 '07 #1
3 1545
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.
--
Terry
"JB" wrote:
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

Sep 14 '07 #2
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 14 '07 #3
JB
On 14 Sep, 19:25, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
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

Sep 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef...
3
by: william | last post by:
Hi everyone, I just started learning VB.NET, I found there are a lots methods passing object parameter by value. For example, Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As...
3
by: GoodMorningSky | last post by:
I have long term question about object serialization. Object serialization is used in many ways. In .net I heard XML is used for object serialization. I understand how object values are serialized...
5
by: Iams | last post by:
Forgive my newbie question, but I guess I don't understand passing parameters, in my case an array. I have a procedure which calls another procedure, passing an array. The new procedure creates...
7
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
16
by: Richard | last post by:
Hi, I am passing a structure to a subroutine where the passed parameter has been declared as ByVal. However, changes made to the passed variable inside the subroutine flow through to the...
14
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.