-1

Suppose I have the following objects:

ZeroObject A = new ZeroObject("A");
ZeroObject B = new ZeroObject("B");
ZeroObject C = new ZeroObject("C");
ZeroObject D = new ZeroObject("D");

When I do:

B = A;

somewhere in the code, B should have a reference of A, right?

Then:

C = B;

that means C is reference of B. Is there a way to give C the reference of A without using:

C = A;
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Zero
  • 118
  • 3
  • 14

6 Answers6

1

Firstly, I'm assuming ZeroObject is a class. If it's not, please disregard this answer.

when I do

B = A;

somewhere in the code,

B should have a reference of A, right?

Well, it means that B will have the same value as A. Any further changes to the value of A (e.g. to make it refer to a different object) will have no effect on A. If you change the data within the object that both A and B refer to, however, you'll be able to see that change through either reference.

Read my article on reference types and value types for more information.

is there a way to give C the reference of A without using

C = A;

in C#?

Well there are bizarre ways you could do it. For example:

public void SetValue<T>(out T target, T value)
{
     target = value;
}

...

SetValue(out C, A);

... but it's unclear why you'd want to do this.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

I'm not convinced inventing some undefined "ZeroObject" is helpful so I'm just going to go ahead and use strings:

string A = "A";
string B = "B";
string C = "C";

//Values are now A, B, C

B = A;

//Values are now A, A, C

C = B;

//Values are now A, A, A
Buh Buh
  • 7,443
  • 1
  • 34
  • 61
0

It depends on what you exactly mean, saying " give C the reference".

If this is about "pointing to the same memory location", it would be enough to assign them to each other.

So

    B = A;
    C = B

C would point to the same A memory space.

But this is kind of really strange work way, honestly. Just assign a reference C=A.

If this is not what you're searching for, please clarify.

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

Is ZeroObject a class of your own?

Programatically, it sounds like you're wanting a referenced object, such as a child object (whatever it may be called in this case).

So, ideally, you want another property on your ZeroObject class, of type ZeroObject - named ReferenceObject (or whatever you prefer), in which you'd assign your sub-object to.

I.e.

public class ZeroObject
{
    //Properties
    ZeroObject ReferenceObject { get; set; }
}

ZeroObject A = new ZeroObject("A");
ZeroObject B = new ZeroObject("B");

A.ReferenceObject = B;

Then simply access B by A.ReferenceObject.

Chris Dixon
  • 9,147
  • 5
  • 36
  • 68
0

Your ZeroObject must have a corresponding property

public class ZeroObject
{
    public ZeroObject (string name)
    {
        Name = name;
    }

    public ZeroObject Parent { get; set }

    public string Name { get; private set; }
}

now, you can write

ZeroObject A = new ZeroObject("A");
ZeroObject B = new ZeroObject("B");
ZeroObject C = new ZeroObject("C");
ZeroObject D = new ZeroObject("D");

B.Parent = A;
C.Parent = B;
D.Parent = C;
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

If you want same object without referenced it, check that link and use that basic solution.

Deep cloning objects

Community
  • 1
  • 1
codeGenius
  • 55
  • 1
  • 2
  • 7