471,075 Members | 767 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to Get Parent Object in case of compostion

BL
Hello

I need to get a parent object of an object. I how to get the instance of an
object in which current object created.

is it possible in c# ?

Thank


Nov 2 '06 #1
5 11413
>I need to get a parent object of an object. I how to get the instance of an
>object in which current object created.

is it possible in c# ?
The easiest way is to pass it in as an argument to the constructor.
You have to keep in mind that the object may be created from a static
method in which case there is no "parent object".
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 2 '06 #2
BL
is it not posiible by reflection or such other thing ? I don't able to pass
it in cunstruction due to some reason ?

please help

BL

"Mattias Sjögren" wrote:
I need to get a parent object of an object. I how to get the instance of an
object in which current object created.

is it possible in c# ?

The easiest way is to pass it in as an argument to the constructor.
You have to keep in mind that the object may be created from a static
method in which case there is no "parent object".
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 2 '06 #3

"BL" <BL@discussions.microsoft.comwrote in message
news:B8**********************************@microsof t.com...
is it not posiible by reflection or such other thing ? I don't able to
pass
it in cunstruction due to some reason ?

please help
Are you referring to a struct or a class? A class instance is not actually
created "in" another object, it is created independently in the managed
heap. A struct may either be created "in" a class instance on the heap,
"in" a box or array independently on the heap, in global memory, or on the
stack, or "in" another struct which can be again in any of these locations.

In native C++, and it was directly declared exactly once and inside only one
class, then you could use __offsetof to get to the parent pointer.

In the managed world, if you are talking about a struct on the managed heap
(by embedding inside a class, box, or array), then one ought to be able to
get a handle to the enclosing allocation unit tracked by the garbage
collector (the one which pinning the struct would fix in place).

If you are going to get any help, you need to show some code, because your
grammar and spelling are too poor for us to understand exactly what you
mean.
>
BL

"Mattias Sjögren" wrote:
>I need to get a parent object of an object. I how to get the instance
of an
object in which current object created.

is it possible in c# ?

The easiest way is to pass it in as an argument to the constructor.
You have to keep in mind that the object may be created from a static
method in which case there is no "parent object".
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 2 '06 #4
BL

Hi

Sorry for mistakes

The following is an example of my requirement. could you please help me what
code/logic I should be write in GetParent() method to get it run ?

public class A()
{
public A{}

B objB = new B();
}

Public Class B()
{
public B()

A objParentofThis = GetParent(this)

private A GetParent(B ObjB)
{
// want to write some code here so this function
// should be return the instance of parent object of this object.
//I am sure the parent object type will be "A"
}

}

BL

"Ben Voigt" wrote:
>
"BL" <BL@discussions.microsoft.comwrote in message
news:B8**********************************@microsof t.com...
is it not posiible by reflection or such other thing ? I don't able to
pass
it in cunstruction due to some reason ?

please help

Are you referring to a struct or a class? A class instance is not actually
created "in" another object, it is created independently in the managed
heap. A struct may either be created "in" a class instance on the heap,
"in" a box or array independently on the heap, in global memory, or on the
stack, or "in" another struct which can be again in any of these locations.

In native C++, and it was directly declared exactly once and inside only one
class, then you could use __offsetof to get to the parent pointer.

In the managed world, if you are talking about a struct on the managed heap
(by embedding inside a class, box, or array), then one ought to be able to
get a handle to the enclosing allocation unit tracked by the garbage
collector (the one which pinning the struct would fix in place).

If you are going to get any help, you need to show some code, because your
grammar and spelling are too poor for us to understand exactly what you
mean.

BL

"Mattias Sjögren" wrote:
I need to get a parent object of an object. I how to get the instance
of an
object in which current object created.

is it possible in c# ?

The easiest way is to pass it in as an argument to the constructor.
You have to keep in mind that the object may be created from a static
method in which case there is no "parent object".
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 3 '06 #5
BL <BL@discussions.microsoft.comwrote:
Sorry for mistakes

The following is an example of my requirement. could you please help me what
code/logic I should be write in GetParent() method to get it run ?
You can't, without passing it in as a parameter to the constructor.
Objects don't remember who created them normally.
--
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
Nov 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by gsb | last post: by
3 posts views Thread by Maximiliano | 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.