Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I set default value to an attribut in the parent constructor?

I have two constructor :

public abstract class A
{
    public A(int a,int b = 0 ) {}
} 

public class B : A
{
    public B(int a , int b):base(a,b) {} // I would like to didn't call again the attribute b 
       // because it's set to 0 in the parent class 
      //   public B(int a):base(a) {} 
}

Maybe my question was ridiculous but I’m searching for a trick to avoid the repeat of calling an attribute with default set to 0 in the parent class, In fact, I have many child classes

Is it possible to do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Update :

This is an instance of B created in MainWindow :

B b1 = new B(1); // a = 1 and b = 0 by default 
B b2 = new B(2,1) // I would like to update the value b and set it to 1 for in the second instance created , In this case I got an error 
// B does not contains a constructor that takes 2 arguments ...

>Solution :

The only way to do this would be to overload the constructor in B:

public class B : A
{
    public B(int a) : base(a) { }
    public B(int a, int b) : base(a, b) { }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading