Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++

Name: Anonymous 2013-11-17 8:50

If i want to create a class B defined in a class A, how do i adress it properly?

If i got something like
ClassA = new A()
Do i create a class B with "ClassA.B() = ClassB" or "ClassA::A() = ClassB" or some thing totally different?

Thanks!

Name: Anonymous 2013-11-17 9:16

Technically exposing class B as a property on class A and setting it works (your first idea, but you got the syntax wrong).

// c#
class Foo
{
    public Bar bar { get; set;}
}

var foo = new Foo();
foo.bar = new Bar();

This isn't a great idea though, as inside the class A you have to depend on external code to have B set, so you have to test if B isn't null all the time.

A better way would be to inject class B in the constructor of A. That way you are using dependency injection as a bonus, which is a SOLID design principle.

var a = new A(new B());

a.B.DoSomething()

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List