How to pass an instance of of a class from within that class

Beakie

I am wondering about something which can be achieved in c++ but I want to do it in C#.

How can I pass a "pointer" to a class, from within the class that contains the code, allowing me to write code in the form of the Foo.Bar()?

namespace Example
{
    public class Foo
    {
        MainClass MainClass;

        public void Bar()
        {
            var somethingElse = MainClass.A.SomethingElse;
            var somethingDifferent = MainClass.B.SomethingDifferent;
        }
    }

    public class MainClass
    {
        public string Something;

        public SubClassA A = new SubClassA(this); // <<-- not valid in c#
        public SubClassB B = new SubClassB(this); // <<-- not valid in c#
    }

    public class SubClassA
    {
        private MainClass _MainClass;

        public SubClassA(MainClass mainClass)
        {
            _MainClass = mainClass;
        }

        public string SomethingElse {
            get {
                return _MainClass.Something + " Else";
            }
        }
    }

    public class SubClassB
    {
        private MainClass _MainClass;

        public SubClassB(MainClass mainClass)
        {
            _MainClass = mainClass;
        }

        public string SomethingDifferent
        {
            get
            {
                return _MainClass.Something + " Different";
            }
        }
    }
}

I should state that I know I can restructure my code in a million ways to achieve a workable result, but I have a specific question I am hoping to answer! :)

Thanks

Jon Skeet

All you need to do is move the initialization expression out of the field declaration and into a constructor:

public class MainClass
{
    public string Something;

    public SubClassA A;
    public SubClassB B;

    public MainClass()
    {
        A = new SubClassA(this);
        B = new SubClassB(this);
    }
}

Note:

  • Please stop using public fields. They make me sad, and it's a Friday afternoon, when I should be happy.
  • "Leaking" a this pointer during construction can be dangerous. If whatever you're calling (the constructors in this case) calls back into the object, it will be running code when you haven't finished initializing. It's a bit like calling a virtual method in a constructor - it works, but it makes the code brittle.
  • Subclass. You keep using this word. I do not think it means what you think it means.
  • You never initialize Foo.MainClass, so your Bar() method is going to fail with a NullReferenceException

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to refer to the instance of a class from within the instance

How to declare instance of a class within a instance of class?

How to create a new class instance from within an instance?

Python how to pass class function from instance to another class

How to pass all class variables from a parent *instance* to a child class?

Python: How do I pass a value for an attribute for an instance of a class that is being created within an instance of a different class?

How to remove an instance within a class

how to create a new instance of a class from within a method?

How to pass class instance to LowLevelMouseProc?

How to pass class instance as parameter?

How to pass a class instance as parameter

How can I create a new class instance from a class within a (static) class?

How to pass functions from class instance into array in Swift

How to pass a value from an EnvironmentObject to a class instance in SwiftUI?

Get Class Instance from Within Decorator

Remove instance of a class from a vector within a vector

Referencing instance variables from within abstract class

Failed to pass the value of an instance variable to another from within the same class (Ruby)

How do I create an instance of a nested class from within that class in Python?

How to initialize an array instance of the class within it self?

How to Serialise a List of instance nested within a class?

How to create instance of a class (object) within for loop?

How to create a class instance within a function?

How to pass an instance of a class into attributes within its constructor and have those attributes access initialized attributes?

Python -- calling a function in same file as a class from within an instance of that class?

Pass output from class instance as input to another

How to create an instance of mocked class within another class

Python: how to get create instance of a class within same class?

How to clone a child class instance within a parent class method