XML weird ??

  • Thread starter Thread starter Vuong
  • Start date Start date
V

Vuong

hi all

here my situation :

public class A
{
XmlDocument m_xmlDocument=null;
public A()
{
this.createEmptyXMLDocument();
// create an empty XMLdocument with XMLDeclaration and
// root Element into x_xmlDocument variable

}

// got a button ----
private void btn_new_click(object sender,System.EvenArgs e)
{
// add new employee
this.addEmployee(this.m_xmlDocument,strFirstname);
}
private addEmployee(XMLDocument doc,string firstname)
{
....... Do something to add new child into "doc" ;
}

}

What i got here is that, not just "doc" is added new child, "m_xmlDocument"
also is added new child , just the same as doc.
As far as i know, there's no way that can happen ....

Thanks,
 
As far as i know, there's no way that can happen ....
Surely that happens. You pass a reference type as pamameter, then
modify its content. doc and m_xmlDocument reference the same memory
location, as the result, the child is inserted into m_xmlDocument. I
suggest you consult documentation about passing reference type as
parameter.

Thi - http://thith.blogspot.com
 
How about this situation ??

public class A
{
int i=0;
public A()
{

}
private void btn_plus_click(object sender,System.EvenArgs e)
{
this.plus(i,2);
// i is still 0 here ;
}
public void plus(int a,int b)
{
a=a+b;
}
}
 
Yes, because int is of value type. XmlDocument is reference type. There
are many good articles on C# parameter passing. Do a search with your
favorite search engine.
 
Lenard said:
int (or System.Int32 what it really is) is a value type. Value types are
passed by value and not by reference.

Whoa there - both reference type expressions and value type expressions
are passed by value by default. It's just that with reference type
expressions, the actual object is not passed at all - only a reference
is passed.

It's very important to maintain a distinction between passing a
reference by value and passing a value by reference.

See http://www.pobox.com/~skeet/csharp/parameters.html

Even if Int32 were a reference type, the "plus" method given wouldn't
change the passed value, because:

a = a+b;

would change the value of the (local) variable a, it wouldn't change
any data within the object which a originally referred to.

(Try it using strings instead of ints - string is a reference type.)

Jon
 
Even if Int32 were a reference type, the "plus" method given wouldn't
change the passed value.
Ah, yes. I did not paid much attention to the snippet. Int32 is just as
immutable as string. It does not expose any methods allowing its
contents to be modified.
 
Back
Top