473,657 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function parameter gets changed

For some reason myFunction in the below code is treating the parameter
oInputDoc1 as a ByRef rather than a ByVal. When the call returns, both
oInputDoc and oOutputDoc = "<root><name>St eve Jobs</name></root>"

When using a string instead of an xml document as the parameter the
argument and value remain ByVal and the results are as I would expect.


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
Dim s As String = "<root><name>Bi ll Gates</name></root>"
Dim oInputDoc As System.Xml.XmlD ocument = New
System.Xml.XmlD ocument
Dim oOutputDoc As System.Xml.XmlD ocument = New
System.Xml.XmlD ocument
oInputDoc.LoadX ml(s)
oOutputDoc = myFunction(oInp utDoc)
Response.Write( oInputDoc.Outer Xml)
Response.Write( "<BR>")
Response.Write( oOutputDoc.Oute rXml)
End Sub

Private Function myFunction(ByVa l oInputDoc1 As System.Xml.XmlD ocument)
As System.Xml.XmlD ocument
Dim s As String = "<root><name>St eve Jobs</name></root>"
oInputDoc1.Load Xml(s)
Return oInputDoc1
End Function

Nov 21 '05 #1
6 1082
Hi,

See this excellent explanation on parameter passing by Jon skeet & you
will understand that it works exactly as it should

http://www.yoda.arachsys.com/csharp/parameters.html

HTH
Kalpesh

Nov 21 '05 #2
I am having the same issue. Let me know if you find a solution.

"df******@parke r.com" wrote:
For some reason myFunction in the below code is treating the parameter
oInputDoc1 as a ByRef rather than a ByVal. When the call returns, both
oInputDoc and oOutputDoc = "<root><name>St eve Jobs</name></root>"

When using a string instead of an xml document as the parameter the
argument and value remain ByVal and the results are as I would expect.


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
Dim s As String = "<root><name>Bi ll Gates</name></root>"
Dim oInputDoc As System.Xml.XmlD ocument = New
System.Xml.XmlD ocument
Dim oOutputDoc As System.Xml.XmlD ocument = New
System.Xml.XmlD ocument
oInputDoc.LoadX ml(s)
oOutputDoc = myFunction(oInp utDoc)
Response.Write( oInputDoc.Outer Xml)
Response.Write( "<BR>")
Response.Write( oOutputDoc.Oute rXml)
End Sub

Private Function myFunction(ByVa l oInputDoc1 As System.Xml.XmlD ocument)
As System.Xml.XmlD ocument
Dim s As String = "<root><name>St eve Jobs</name></root>"
oInputDoc1.Load Xml(s)
Return oInputDoc1
End Function

Nov 21 '05 #3
Passing an object reference ByVal just protects the reference from changing,
not the state of the object. i.e., you're not passing the entire object,
just the object reference.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
"df******@parke r.com" wrote:
For some reason myFunction in the below code is treating the parameter
oInputDoc1 as a ByRef rather than a ByVal. When the call returns, both
oInputDoc and oOutputDoc = "<root><name>St eve Jobs</name></root>"

When using a string instead of an xml document as the parameter the
argument and value remain ByVal and the results are as I would expect.


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
Dim s As String = "<root><name>Bi ll Gates</name></root>"
Dim oInputDoc As System.Xml.XmlD ocument = New
System.Xml.XmlD ocument
Dim oOutputDoc As System.Xml.XmlD ocument = New
System.Xml.XmlD ocument
oInputDoc.LoadX ml(s)
oOutputDoc = myFunction(oInp utDoc)
Response.Write( oInputDoc.Outer Xml)
Response.Write( "<BR>")
Response.Write( oOutputDoc.Oute rXml)
End Sub

Private Function myFunction(ByVa l oInputDoc1 As System.Xml.XmlD ocument)
As System.Xml.XmlD ocument
Dim s As String = "<root><name>St eve Jobs</name></root>"
oInputDoc1.Load Xml(s)
Return oInputDoc1
End Function

Nov 21 '05 #4
CT
Check this out,
http://msdn.microsoft.com/library/de...ingbyvalue.asp.

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Communities - http://community.integratedsolutions.dk

<df******@parke r.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
For some reason myFunction in the below code is treating the parameter
oInputDoc1 as a ByRef rather than a ByVal. When the call returns, both
oInputDoc and oOutputDoc = "<root><name>St eve Jobs</name></root>"

When using a string instead of an xml document as the parameter the
argument and value remain ByVal and the results are as I would expect.


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
Dim s As String = "<root><name>Bi ll Gates</name></root>"
Dim oInputDoc As System.Xml.XmlD ocument = New
System.Xml.XmlD ocument
Dim oOutputDoc As System.Xml.XmlD ocument = New
System.Xml.XmlD ocument
oInputDoc.LoadX ml(s)
oOutputDoc = myFunction(oInp utDoc)
Response.Write( oInputDoc.Outer Xml)
Response.Write( "<BR>")
Response.Write( oOutputDoc.Oute rXml)
End Sub

Private Function myFunction(ByVa l oInputDoc1 As System.Xml.XmlD ocument)
As System.Xml.XmlD ocument
Dim s As String = "<root><name>St eve Jobs</name></root>"
oInputDoc1.Load Xml(s)
Return oInputDoc1
End Function

Nov 21 '05 #5
dferr...@parker .com wrote:
For some reason myFunction in the below code is treating the parameter
oInputDoc1 as a ByRef rather than a ByVal. When the call returns, both
oInputDoc and oOutputDoc = "<root><name>St eve Jobs</name></root>"
It is working just as it is supposed to. In your function, what gets
passed is a copy of the *reference* to the XmlDocument, not a copy of
the document itself. Thus, you have two different references to the
same object. So, inside your function, if you were to change the
*reference*, then the original reference would not be affected:

If you code were to do this:
Private Function myFunction(ByVa l oInputDoc1 As System.Xml.XmlD ocument)
As System.Xml.XmlD ocument oInputDoc1 = New XmlDocument End Function


Then your original document would not be affected.

When you pass a reference type ByVal, you get a copy of the reference.
When you pass a reference type ByRef, you pass the actual reference.

Either way, any changes you make to the properties,etc. will affect the
original object that each points to.

Nov 21 '05 #6
<df******@parke r.com> schrieb:
For some reason myFunction in the below code is treating the parameter
oInputDoc1 as a ByRef rather than a ByVal. When the call returns, both
oInputDoc and oOutputDoc = "<root><name>St eve Jobs</name></root>"


See:

<URL:http://groups.google.d e/group/microsoft.publi c.dotnet.langua ges.vb/msg/25064a4fedd3663 3>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7

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

Similar topics

39
7638
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down indicate: a) That I don't know enough b) Passing arguments by ref is bad
1
3339
by: jimfortune | last post by:
From: http://groups-beta.google.com/group/comp.databases.ms-access/msg/769e67e3d0f97a90?hl=en& Errata: 19 solar years = 2939.6018 days should be 19 solar years = 6939.6018 days Easter Function explanation Part II
11
4724
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to create content to put at the address pointed to by f. In particular, I'd like to avoid/replace the memcpy line. Possible application (inspired by Paul Graham, "ANSI Common Lisp",
64
3371
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be "refactor-ed" to be
20
10741
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
16
3153
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed inside the function. And it is for this reason that some people advocate that it is a good idea to...
4
2487
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec 2006) $ Author: Collin Winter <collinw@gmail.com>, Tony Lownds <tony@lownds.com> Status: Draft Type: Standards Track
10
13012
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var boldLink=document.getElementById('cmtbold');
14
1545
by: Nickolay Ponomarev | last post by:
Hi, Why does http://www.jibbering.com/faq/ uses new Function constructor instead of function expressions (function(...) { ... }) when defining new functions? E.g. for LTrim and toFixed. Is the reason that you want to avoid accidental closures? Or does some obscure browser does not support function expressions? This just looks weird, given that http://www.jibbering.com/faq/#FAQ4_40 says to only use eval() (which is not really much...
0
8306
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,...
0
8825
Oralloy
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...
0
8732
jinu1996
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...
0
5632
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();...
0
4152
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...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
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...

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.