473,405 Members | 2,282 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,405 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 2657
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Paul Morrow | last post by:
I'm hoping that someone can explain why I get the following exception. When I execute the code... ###################################### class Parent(object): class Foo(object): baz = 'hello...
6
by: A | last post by:
Hi, How do you make use of nested functions in C++? I realize in C++ that everything must be declared first in a header file before implementation in a .cpp file. I tried to nest a method...
4
by: Vedanta Barooah | last post by:
greetings.... in a python nested class is it possible to change the value of the parent class's variable without actually creating an instance of the parent class, consider this code: class...
3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
2
by: Quinnie | last post by:
Hi, I have a homework assignment that I'm so confused and really need help with. Here's the description, any help would be appreciated. Thanks! Assume we have a statically-scoped language...
11
by: C# Learner | last post by:
Is it not possible to declare a nested class in a seperate file from its "parent" class -- i.e. in a similar way to the idea of spreading namespaces over more than one file?
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
6
by: Marc Hoeijmans | last post by:
How is it possible to us a property from a nested class. Example: namespace MyApp { public class MaxLength { public class Project {
2
by: GISmatters | last post by:
I have unbound checkboxes in a nested gridview to allow multi-selection of "child" rows. For context, the parent gridview rows are for large "reports", the child rows are for various specific files...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.