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!
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 ...
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.
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.
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.
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();
}
}
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With