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 ChangeValueByVa l(ByVal Test As CTest)
Test.Value = "Value Has been changed by the Sub (ByVal)"
End Sub
Public Sub ChangeValueByRe f(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. ChangeValueByVa l and
ChangeValueByRe f called locally)
'============== =============== =============== =============== =========
Test.Value = "Initial Value"
ChangeValueByVa l(Test)
'Upon exit Test contains "Value Has been changed by the Sub (ByVal)"
'<== OK
ChangeValueByRe f(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. ChangeValueByVa l and
ChangeValueByRe f called remotely)
'============== =============== =============== =============== ============
Test.Value = "Initial Value"
ChangeValueByVa l(Test)
'Upon exit Test contains "Initial Value" '<== Inconsistent, why is
that !!!!
ChangeValueByRe f(Test)
'Upon exit Test contains "Value Has been changed by the Sub (ByRef)"
'<== OK 3 1567
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 ChangeValueByVa l(ByVal Test As CTest)
Test.Value = "Value Has been changed by the Sub (ByVal)"
End Sub
Public Sub ChangeValueByRe f(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. ChangeValueByVa l and
ChangeValueByRe f called locally)
'============== =============== =============== =============== =========
Test.Value = "Initial Value"
ChangeValueByVa l(Test)
'Upon exit Test contains "Value Has been changed by the Sub (ByVal)"
'<== OK
ChangeValueByRe f(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. ChangeValueByVa l and
ChangeValueByRe f called remotely)
'============== =============== =============== =============== ============
Test.Value = "Initial Value"
ChangeValueByVa l(Test)
'Upon exit Test contains "Initial Value" '<== Inconsistent, why is
that !!!!
ChangeValueByRe f(Test)
'Upon exit Test contains "Value Has been changed by the Sub (ByRef)"
'<== OK
>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 MarshalByRefObj ect 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.
On 14 Sep, 19:25, Mattias Sjögren <mattias.dont.w ant.s...@mvps.o rg>
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 MarshalByRefObj ect 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.ne t/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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 points to something and changes it. The default for
simple data types is ByVal and for objects, it's ByRef.
Am I correct so far? If so, I still don't have a clue as
|
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 System.EventArgs) Handles Button3.Click
It's impossible in vb6.In vb6, the object is always passed by reference.
How come we can pass object byval in .NET?
Can anyone explain it?
Thanks.
|
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 and read many books about it.
But, I don't understand how those methods are serialized?
Are instance methods serialized? I think it does so, remoting object method
call is possible.
If so how the method is serialized? Instance values are just...
|
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 the array, but when it returns to the original procedure,
the array is lost, along with all the parameters I want modified by the
called procedure. I thought I understood the default was byref, which would
modify the actual variable, not just a...
|
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
| |
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 changing
the original variable passed)
|
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 actual variable that has been passed over, even though with
ByVal this should not happen.
Has anybody else discovered this or am I doing completely wrong /
|
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 something?
Regards
/Niklas
Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyInt = 1
|
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, as I expected. I
haven't seen this behavior explained precisely in the .net world yet,
so I wanted to check and make sure I've got it right.
I apologize that this is a bit long. I've tried to keep it concise.
There are code fragments here, but...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |