Shaken_U
Shaken_U

Reputation: 162

How to Pass objects between classes

So what I'm trying to do here is pass the same copy of a class (class A) to another class (class B), but class B is instanced in class A.

Using a new statement in class B won't work because it would cause an infinite loop, as well as creating a new instance of it, when I want to be able to use variables from the 1st instance of class A.

I know about object.equals() but I can't use it until I define the class A's object in class B. Just using object.equals results in a null reference.

public partial class class_A : Form
{
    public class_B _class_B = new class_B;
    public Int32 var; 

    private void setclassA()
    {
        _class_B._class_A.equals(this);
    }
}

public class class_B
{
    public class_A _class_A;        // I know this is null
    // code
}

Like I said I want to avoid instancing a new copy of class A because I want the values in class A to be set.

I've tried using a method to do it but still get a null reference.

Upvotes: 5

Views: 2827

Answers (4)

Shaun Luttin
Shaun Luttin

Reputation: 141712

Answer

You're 75% there. As Ron mentioned, just change .equals(this) to = this as follows:

_class_B._class_A = this;

Explanation

If we want to pass the same copy of ClassA to ClassB, and to do that when we instantiate ClassB inside of ClassA, then we use the this keyword, which represents the current instance of a class.

Though there are other ways to give this to ClassB, the below example shows two:

  1. pass this to the constructor and do the property assignment in ClassB or
  2. assign this directly to a property in ClassB.

The key for you is that = is an assignment operator whereas Equals() checks whether two variables refer to the same object or not.

Example

public class ClassA
{
    public ClassB MyClassB1 { get; set; }
    public ClassB MyClassB2 { get; set; }

    public ClassA()
    {
        // pass `this` to the constructor
        this.MyClassB1 = new ClassB(this);

        // pass `this` directly to a property in `ClassB`
        this.MyClassB2 = new ClassB();
        this.MyClassB2.MyClassA = this;
    }
}

public class ClassB
{
    public ClassA MyClassA { get; set; }

    public ClassB() { }

    public ClassB(ClassA classA)
    {
        // do property assignment in the constructor
        this.MyClassA = classA;
    }
}

Proof of Concept

This runs as a Fiddle here and prints out "They are the same object," and some other things too in the more recent version.

using System;   
public class Program
{
    public static void Main()
    {
        var classA = new ClassA();

        if(classA.Equals(classA.MyClassB1.MyClassA) &&
           classA.Equals(classA.MyClassB2.MyClassA) &&
           classA.MyClassB1.MyClassA.Equals(classA.MyClassB2.MyClassA))
        {
            Console.WriteLine("They are the same object.");
        }
    }
}

An important note is that, when we use this, we're giving ClassB a reference to ClassA not a copy of it. These are very different things.

Upvotes: 3

RagtimeWilly
RagtimeWilly

Reputation: 5445

Pass A in the constructor of B:

public class A
{
   private B _b;

   public A()
   {
       _b = new B(this);
   }
}

public class B
{
    private A _a;

    public B(A a)
    {
        _a = a;
    }
}

As mentioned in the comments you're completely misunderstanding .Equals(). It's used to compare whether two objects are equal not clone / pass references.

Upvotes: 4

Ben
Ben

Reputation: 2523

Use get; set;

In Class A:

public partial class class_A : Form
{
    Class_B B = new Class_B();
    B.Class_A = this;
    public Int32 var;
}   

Then in Class B:

public class class_B
{
    Class_A A { get; set; }
    // code
}

Upvotes: -2

Nguyen Kien
Nguyen Kien

Reputation: 1927

Try this:

public class A
{
    public B Instance_B;

    public A(B b)
    {
         Instance_B = b;
    }
}

public class B
{
    public A Instance_A;

    public B()
    {
         Instance_A = new A(this);
    }
}

Upvotes: 0

Related Questions