473,666 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 UserCurrentLeve l
{
get { return _User.Level;}
}
}
}
}

Jan 7 '06 #1
3 2691
<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.co m>
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.c om 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
4541
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 from Parent.Foo' class Child(Parent): #Foo.baz = 'hello from Child.Foo'
6
12195
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 prototype in another prototype but seems pointless. Can someone please write a short, simple, and concise skeleton code of how to use nested functions? class Foo {
4
4419
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 mother: x=0 def __init__(self): self.x=1
3
2398
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
2346
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 with nested procedures. That is, a procedure (or function) can contain local procedures (and functions). Procedures can be nested arbitrarily deep. The scoping rules for procedure names (i.e., the ability to call
11
2716
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
16890
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. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
6
3103
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
2820
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 comprising each report. I want the user to be able to select an arbitrary collection of report files and have them emailed by clicking an "Email selected files" button. Everything displays properly, including the checkboxes for each child row (each...
0
8355
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8638
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6191
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.