yiannis
yiannis

Reputation: 430

Calling a constructor with arguments from another constructor

I'm trying to call the constructor of class B from class A by passing some arguments (args1,args2). I am using something like this:

public class A
{
       private readonly B _b;
       public A()
       {
         _b=new B(TypeA args1,TypeB args2);
       }

...
}

public class B
{

   public B(TypeA  new_args1,TypeB new_args2)
   {
     ...
   }

...
}

But from what I see in debug althougth args1 and args2 have the correct values that I want to send, new_args1 and new_args2 do not change. Is there a specific syntax I have to use to do that?

Upvotes: 0

Views: 192

Answers (3)

George Mamaladze
George Mamaladze

Reputation: 7931

First of all let's fix syntax:

public class A
{
    private readonly B _b;
    public A(TypeA args1, TypeB args2)
    {
        _b = new B(args1, args2);
    }

}

public class B
{
    public B(TypeA new_args1, TypeB new_args2)
    {

    }

}

Please note that types of arguments must match exactly otherwise another constructor with matching signature may be invoked. Let's say yo have two constructors on B in this case the first one gets invoked and the second one not:

public class B
{
    public B(TypeA new_args1, TypeB new_args2)
    {

    }

    public B(TypeA new_args1, TypeC new_args2)
    {

    }

}

One more point: I would youse DI (Dependency Injection) in this case. Doing construction in constructor is a flaw unless a constructed object is an atomic data structure like List, Dictionary etc.

public class M
{
    public void Main(TypeA new_args1, TypeB new_args2)
    {
        var b = new B(new_args1, new_args2);
        var a = new A(b);
    }
}


public class A
{
    private readonly B _b;
    public A(B b)
    {
        _b = _b;
    }

}

public class B
{
    public B(TypeA new_args1, TypeB new_args2)
    {

    }
}

Upvotes: 1

user1563237
user1563237

Reputation: 1

Well what i can see the error in your code is that you should declare your A constructor with paranthesis i.e. A() and check whether it works. Beside that your code looks absolutely correct.

Upvotes: 0

Eric J.
Eric J.

Reputation: 150178

I'm not sure why you call the args to the constructor of B "new". They are the arguments for instantiating that particular object instance.

Except for the fact that you're missing a type in the argument declaration, your code looks correct. What exactly is wrong.

public B(new_args1,new_args2)

is missing the types, e.g.

public B(int new_args1, int new_args2)

Given the type assumption above

_b=new B(42, 24);

would cause B to be initialized as

public B(int new_args1, int new_args2)
{
    // new_args1 has the value 42
    // new_args2 has the value 24
}

Assuming you assign those values somewhere in B, e.g.

public class B 
{
    public int A1 { get; private set; }
    public int A2 { get; private set; }
    public B(int new_args1, int new_args2)
    {
        // new_args1 has the value 42
        A1 = new_args1;
        // new_args2 has the value 24
        A2 = new_args2;
    }
}

then

_b.A1 

would have the value 42, and

_b.A2

would have the value 24

after you initialize _b.

Upvotes: 2

Related Questions