473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

reference type parameter and property

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?

Feb 14 '07 #1
3 1724
<jb****@transferflow.comwrote:
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.
<snip>
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?
You haven't fully understood reference types and reference parameters.

Hopefully the following might help. The second one is very much a work
in progress:

http://pobox.com/~skeet/csharp/parameters.html
http://pobox.com/~skeet/csharp/references.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '07 #2
On Feb 14, 1:40 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
<jba...@transferflow.comwrote:
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.

<snip>
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?

You haven't fully understood reference types and reference parameters.

Hopefully the following might help. The second one is very much a work
in progress:

http://pobox.com/~skeet/csharp/param...eferences.html

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
I still don't see how would I get ChangeValue( ) to make foo = 1. If
I set myObject to 1 in Class1's constructor then foo should be 1 but I
don't see a way to do it using the _myObject.

Feb 14 '07 #3
jb****@transferflow.com <jb****@transferflow.comwrote:
You haven't fully understood reference types and reference parameters.

Hopefully the following might help. The second one is very much a work
in progress:

I still don't see how would I get ChangeValue( ) to make foo = 1. If
I set myObject to 1 in Class1's constructor then foo should be 1 but I
don't see a way to do it using the _myObject.
You can't, basically. You *could* create an ObjectWrapper class like
this:

public class ObjectWrapper
{
object value;

public object Value
{
get { return value; }
set { this.value = value; }
}
}

You could change the constructor to take an ObjectWrapper, store that,
and then change the contents at a later date.

It would rarely be a good design decision, however. Now, what higher
goal are you trying to achieve?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '07 #4

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

Similar topics

6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
1
by: Wayne | last post by:
How do you pass an object reference to a property during design? In other words, I would like to type the name of the object in on the properties window and then use that object to do neat stuff....
3
by: Ross McLean | last post by:
Hi all, I've been teaching myself C# for a new project at work. I have a bit of a background in c++ and java but never been what you could call a guru. I'm having some strange things happening...
5
by: hamerb | last post by:
So the line of code I'm working on is something like this: func(ref obj.GetType().GetProperty(PopertyNameSring).GetValue(obj, null)) This ofcourse doesn't work since the GetValue method gets...
4
by: Ole Nielsby | last post by:
Here comes a generic class with a static property. Let's say they are exotic singleton pets. abstract class Pet {...} abstract class SharedPet<T>: Pet where T: SharedPet<T>, new() {...
9
by: Edward Diener | last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference to a reference in C#. I know one can use it on a value type to create a reference to that value.
1
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
1
by: subramanian100in | last post by:
consider template<typename TTest { // ... }; We can have a pointer type as argument to a template class. For example, we can have, int x = 100; Test<int*obj(&x); // assuming a suitable ctor...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.