J
jbaldi
I am trying to pass an object as a reference parameter in a
constructor. I would like my class with the reference parameter
constructor to be able to change the value of that object from one of
its methods as in the following example.
public class Class1 {
private object _myObject;
public Class1(ref object myObject) {
_myObject = myObject;
}
public void ChangeValue( ) {
_myObject = 1;
}
}
public class Class2 {
public Class2( ) {
object myObject = 0;
Class1 myClass1 = new Class1(ref myObject);
myClass1.ChangeValue( );
object foo = myObject;
}
}
My problem is that foo will have a value of 0 instead of 1. I thought
that objects were reference types so this should work. What am I
missing?
constructor. I would like my class with the reference parameter
constructor to be able to change the value of that object from one of
its methods as in the following example.
public class Class1 {
private object _myObject;
public Class1(ref object myObject) {
_myObject = myObject;
}
public void ChangeValue( ) {
_myObject = 1;
}
}
public class Class2 {
public Class2( ) {
object myObject = 0;
Class1 myClass1 = new Class1(ref myObject);
myClass1.ChangeValue( );
object foo = myObject;
}
}
My problem is that foo will have a value of 0 instead of 1. I thought
that objects were reference types so this should work. What am I
missing?