tags:

views:

124

answers:

8
  1. I have a base class, "B", which has two constructors, one with no paremeters and the other that accepts one param, an integer
  2. I have a subclass, "S", which inherits from "B" and does not define any constructors in it.
  3. I create an instance of S, attempting to pass to the constructor an integer.

I get the error:

Error 1 Too many arguments to 'Public Sub New()"

This surprises me because I thought that if a sub is not defined in the subclass, S, that the base class constructor method, specifically, the one with the single integer param would be invoked w/o an error.

Do you have any idea why I am getting this error? Are constructors a special case?

+1  A: 

No. That is an incorrect assumption.

In case you don't define a constructor, a parameterless constructor is added.

shahkalpesh
Not sure what you are saying I've assumed incorrectly. I think my assumption was correct.
Velika
A: 

If you don't add a constructor to a class explicitly, it has a no-argument empty constructor by default. And I think this answers your question :)

Aamir
Yeah, I knew that, but that doesn't explain why I couldn't call the constructor in the base without there being a constructor defined in the subclass that maps to it.
Velika
A: 

Generally the constructor of the base class is not public. Rather, it is typically called from the constructor of the subclass.

All classes have a constructor, whether you define them or not. In your case, I suspect that your call to the constructor of the subclass fails because there is no constructor defined in the subclass that takes one parameter.

Robert Harvey
No, as the question states, there WAS a constructor in the base class that accepts one param. See the answer. Thanks to All!
Velika
I guess I didn't explain it well. You are agreeing with me.
Robert Harvey
+1  A: 

That is because for any class if you do not define a constructor the compiler considers a default parameterless constructor is present. Also you wont be able to set any private base class members unless in your derived class you explicitly call the base class constructor.

This is the C# code. VB should be similar.

class B
    {
        protected int aInt;
        private int bInt;
        public B()
        { }

        public B(int myInt)
        {
            aInt = myInt;
        }
    }

    class S : B
    {
        public S(int aInt, int bInt)
            : base(bInt)
        {
            base.aInt = aInt;
        }
    }
Rashmi Pandit
Very good pt. I almost selected this one as the correct answer. Thanks
Velika
Thank you. I was trying to post vb code, but I found it little difficult and gave up!
Rashmi Pandit
+5  A: 

The constructor for a class only applies to the class it's defined in, even in an inheritance relationship. To be able to take advantage of your base-class 1-arg constructor, you'd have to do something like this (in C#):

public class S : B
{
    public S()
    {
        // do something for S
    }

    public S(int myInt) : base(myInt)
    {
        // do something for S
    }
}
Andy White
Another runner up answer. Thanks for answering.
Velika
+5  A: 

As you suspect, constructors are not inherited. Subclasses need to implement their own constructors. The reason is that a base class's constructors will not know how to initialize the subclass's members, so having them invoked would put the subclass object in a bad state.

John Kugelman
The last 3 answers were pretty close, I voted them all up but picked this one. Thanks!
Velika
+2  A: 

It looks like you are using VB. In which case, you need to explicitly call the appropriate base class constructor from the subclass's constructor.

When an instance of a derived class is created, the Sub New constructor of the base class executes first, followed by constructors in derived classes. Link to page

It is calling the parameterless base class constructor, but sending an integer since you are not explicitly calling the base class constructor that accepts an integer from the derived class constructor.

ecounysis
A: 

The first statement in a constructor must be mybase.New. The reason for this is that all classes inherit from Object class and a constructor must call its base class' constructor.

zdmytriv