471,627 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Nested Object passed Parent by ref

Hello...

I have an object which has a nested object which need to look at the
parent to return a value
about it. Basically as below but when I put the ref method parameter
to construct the nested class, I get the error message about "this"
(the parent being readonly).

When I leave out the ref keyword things compile, but I want to make
sure I pass by reference so I can save memory. As you can see the
parent is passed but not modified by the nest class. Any ideas?

namespace test
{
public class User
{
private Status _Status;
private string _Level

public User()
{
this._Status = new Status(ref this);
}

public string Level
{
get { return _Level;}
set {_Level = value;}
}

public class Status
{
private User _User;

public Status(ref User usr)
{
_User = usr;
}

public string UserCurrentLevel
{
get { return _User.Level;}
}
}
}
}

Jan 7 '06 #1
3 2509
<mb*****@gmail.com> wrote:
I have an object which has a nested object which need to look at the
parent to return a value
about it. Basically as below but when I put the ref method parameter
to construct the nested class, I get the error message about "this"
(the parent being readonly).

When I leave out the ref keyword things compile, but I want to make
sure I pass by reference so I can save memory. As you can see the
parent is passed but not modified by the nest class. Any ideas?


Yes - I don't think you understand what "pass by reference" really
means. There's no need to pass anything by reference here; instead, you
want to pass the reference by value, which is the default behaviour.

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

--
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
Jan 7 '06 #2
OK so because a class is being passed into the constructor, it defaults
to value....

What I am trying to find-out here is that if it is passed by value,
then a new copy of the object
is created, this would not be ideal, as my User object, although not
shown in the code snippet,
is actually very large, holding multiple collection classes.

What is best for conserving memory?

Thanks,
Matt

Jan 9 '06 #3
mb*****@gmail.com wrote:
OK so because a class is being passed into the constructor, it defaults
to value....
No - it's got nothing to do with the fact that it's being passed into a
constructor.
What I am trying to find-out here is that if it is passed by value,
then a new copy of the object is created, this would not be ideal,
as my User object, although not shown in the code snippet,
is actually very large, holding multiple collection classes.

What is best for conserving memory?


No, you don't understand. A new copy of the object is *not* created
(unless the User object is a value type, which I hope it's not).

Did you read the link I posted? It explains all this in a fair amount
of detail. If that hasn't helped you to understand that a new copy of
the object isn't created, it sounds like it needs more work. You'll
need to read it thoroughly rather than just skimming it though.

Jon

Jan 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Paul Morrow | last post: by
6 posts views Thread by A | last post: by
4 posts views Thread by Vedanta Barooah | last post: by
3 posts views Thread by Erik Bongers | last post: by
2 posts views Thread by Quinnie | last post: by
11 posts views Thread by C# Learner | last post: by
8 posts views Thread by Robert W. | last post: by
6 posts views Thread by Marc Hoeijmans | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.