Unexpected mutation of variable in LINQ (VB.NET)

  • Thread starter Thread starter mike
  • Start date Start date
M

mike

I have a function that is being called from code. The XDocument that I
am passing into the function is being mutated and I would like to know
why. See the example below...

Class Myclass
Public Sub CreateXml
Dim siteDoc = XDocument.Load(siteXmlPath)

Dim contentWithoutSectionElements As XDocument =
RemoveSectionsFromDocument(siteDoc)
End Sub

Private Function RemoveSectionsFromDocument(ByVal srcdoc As
XDocument) As XDocument
srcdoc...<section>.Remove()

Return srcdoc
End Function
End Class

In the debugger, siteDoc now no longer has <section> elements. It's
like the parameter has been passed ByRef rather than ByVal.

Hope someone can answer this. I'm completely baffled by this one.

Thanks
Mike
 
I have a function that is being called from code. The XDocument that I
am passing into the function is being mutated and I would like to know
why. See the example below...

Class Myclass
Public Sub CreateXml
Dim siteDoc = XDocument.Load(siteXmlPath)

Dim contentWithoutSectionElements As XDocument =
RemoveSectionsFromDocument(siteDoc)
End Sub

Private Function RemoveSectionsFromDocument(ByVal srcdoc As
XDocument) As XDocument
srcdoc...<section>.Remove()

Return srcdoc
End Function
End Class

In the debugger, siteDoc now no longer has <section> elements. It's
like the parameter has been passed ByRef rather than ByVal.

Hope someone can answer this. I'm completely baffled by this one.

I'm baffled why a question stated entirely in VB.NET is posted to the C#
newsgroup. :p

As for what's going on, you appear to be confused by the difference
between a reference type and value type, versus the difference between
passing an argument by reference or by value.

You are passing a reference type by value, which means the value that's
passed is a reference to the object. Since the argument is passed by
value, you can't change whatever variable contained the reference to the
object. But because the value passed is a reference, changes to the
_object_ itself are seen anywhere you have a reference to the object. And
of course, both the caller and callee have the same reference to the same
object.

Since you posted VB.NET code, I have no idea whether you'll be able to
understand this, but Jon Skeet wrote a good article on the topic:
http://www.yoda.arachsys.com/csharp/parameters.html

Pete
 
* Peter Duniho wrote, On 16-9-2009 9:35:
I'm baffled why a question stated entirely in VB.NET is posted to the C#
newsgroup. :p

As for what's going on, you appear to be confused by the difference
between a reference type and value type, versus the difference between
passing an argument by reference or by value.

You are passing a reference type by value, which means the value that's
passed is a reference to the object. Since the argument is passed by
value, you can't change whatever variable contained the reference to the
object. But because the value passed is a reference, changes to the
_object_ itself are seen anywhere you have a reference to the object.
And of course, both the caller and callee have the same reference to the
same object.

Since you posted VB.NET code, I have no idea whether you'll be able to
understand this, but Jon Skeet wrote a good article on the topic:
http://www.yoda.arachsys.com/csharp/parameters.html

Pete


If XDocument implements a copy constructor, which you can use to create
direct copies from an existing XDocument.

So instead of calling
RemoveSectionsFromDocument(siteDoc) ;

you can call
RemoveSectionsFromDocument(new XDocument(siteDoc)) ;

In C# that is. I suck at VB.Net.
 
Thanks for your help Pete.

As for the statement below, I'm baffled too! In my defense Your
Honour, I write in both C# and VB.NET and still get mixed up when I
switch between the two. I guess it applies to the newsgroups too!
I'm baffled why a question stated entirely in VB.NET is posted to the C#  
newsgroup.  :p

Cheers
Mike
 
Back
Top