Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# passing current object into another object?

Tags:

c#

I am creating an object that has a constuctor like this...

public class BusinessLogic()
{

     public BusinessLogic()
      {

           BusinessLogicSubClass blsc = new BusinessLogicSubClass(and I want to pass in BusinessLogic here)
      }
}

I am doing this because I want BusinessLogicSubClass to call back to varying methods within BusinessLogic when it completes one method or another. BusinessLogicSubClass also uses Constructor Injection in order for my Unit Tests to work with NMock2.

Any suggestions here would be helpful thank you in advance!

like image 720
apolfj Avatar asked Feb 03 '09 17:02

apolfj


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


3 Answers

 public BusinessLogic()
  {
       BusinessLogicSubClass blsc = new BusinessLogicSubClass(this);
  }

An alternative to this (to address Jon Skeet's comments) would be to have a constructor, and an initializer, which uses the "this" pointer:

public class BusinessLogic 
{
    private BusinessLogicSubClass blsc = null;

    public BusinessLogic() {}

    public void Initialize()
    {
        blsc = new BusinessLogicSubClass(this);
    }
}

public class Implementor
{
    public void SomeFunction()
    {
        BusinessLogic bl = new BusinessLogic();
        bl.Initialize();
    }
}
like image 88
TheSmurf Avatar answered Sep 30 '22 01:09

TheSmurf


You can use this within a constructor. It's generally not a great idea though, as it means you've published a reference before the object has finished being constructed.

It's not really clear why you're creating a subclass within the class itself though - I suspect your inheritance hierarchy might not be ideal. Could you give some more details, so we might be able to recommend better design patterns?

like image 24
Jon Skeet Avatar answered Sep 30 '22 00:09

Jon Skeet


Can you used lazy instantiation on a property to delay the instantiation of BusinessLogicSubClass for when you need it and when BusinessLogic has completed construction? I think this would be a better design. E.g.

public class BusinessLogic {

    private BusinessLogicSubClass mChild;

    public BusinessLogic() {
    }

    public BusinessLogicSubClass Child {
        get {
            return mChild ?? (mChild = new BusinessLogicSubClass(this));
        }
    }

    public class BusinessLogicSubClass {
        public BusinessLogicSubClass(BusinessLogic parent) {
        }
    }
}

Use lazy instantiation to prevent passing this in the original constructor which is not a good idea for a not properly constructed object - although could be ok depending on what else is going on.

like image 42
Sam Meldrum Avatar answered Sep 30 '22 01:09

Sam Meldrum