ownership chain

  • Thread starter Thread starter Nicholas Paldino [.NET/C# MVP]
  • Start date Start date
N

Nicholas Paldino [.NET/C# MVP]

DaveP,

AFAIK, you can't. You have to keep track of this manually.
 
how do u keep a ownership chain of objects example below
class1=new class1()
class2=new class2()

how can i show a reference back to class2 from within class2
do i send class1 in as a parameter reference

Thanks
DaveP
 
DaveP said:
how do u keep a ownership chain of objects example below
class1=new class1()
class2=new class2()

how can i show a reference back to class2 from within class2
do i send class1 in as a parameter reference

That sentence doesn't make a great deal of sense.

Do you mean a reference back to class1 from within class2?
Your sample code shows no ownership at all.
Perhaps you can pseudocode something that looks like what you want to
do.

Here is a simple program demonstrating an object holding a reference to
another object.
I don't know if this is what you are after...good luck

using System;
class Foo
{
static void Main(string[] args)
{
A a = new A();
a.X=5;
B b = new B(a);
Console.WriteLine("b.Foo():{0}",b.Foo());
a.X = 7;
Console.WriteLine("b.Foo():{0}",b.Foo());
}
}

public class A
{
private int x;
public int X { get{return x;} set {x = value;}}
}

public class B
{
private A a;
public B(A a)
{
this.a = a;
}
public int Foo()
{
return a.X;
}
}
 
yes thats what im looking for...
Its also the way that im keeping a reference to the owning class (calling)
class...still new with c#
making sure im on the right direction
Thanks
DaveP

Bill Butler said:
DaveP said:
how do u keep a ownership chain of objects example below
class1=new class1()
class2=new class2()

how can i show a reference back to class2 from within class2
do i send class1 in as a parameter reference

That sentence doesn't make a great deal of sense.

Do you mean a reference back to class1 from within class2?
Your sample code shows no ownership at all.
Perhaps you can pseudocode something that looks like what you want to do.

Here is a simple program demonstrating an object holding a reference to
another object.
I don't know if this is what you are after...good luck

using System;
class Foo
{
static void Main(string[] args)
{
A a = new A();
a.X=5;
B b = new B(a);
Console.WriteLine("b.Foo():{0}",b.Foo());
a.X = 7;
Console.WriteLine("b.Foo():{0}",b.Foo());
}
}

public class A
{
private int x;
public int X { get{return x;} set {x = value;}}
}

public class B
{
private A a;
public B(A a)
{
this.a = a;
}
public int Foo()
{
return a.X;
}
}
 

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

Similar Threads

hide caption/title bar 6
Sql Connection 9
dataset, datatable, dataadapter 4
character conversion 2
DLL and Application Config 1
Calling C#Dll from Vb6 7
Math 9
Serial Communications 2

Back
Top