Nested Object passed Parent by ref

M

mbaskey

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;}
}
}
}
}
 
J

Jon Skeet [C# MVP]

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.
 
M

mbaskey

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
 
J

Jon Skeet [C# MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top