Class Inheritance

B

Bob lazarchik

I am trying to derive a class from an existing class with parameters in the
constructor. Does anyone know why this is happening.

The Code

public class ClassA
{
public ClassA( int Param1 )
{
}
}


public class ClassB : ClassA
{
public ClassB( int Param1 )
{
}
}

I get a "No overload for method 'ClassA' takes '0' arguments" compiler
error. But if i change the class A constructor to as below the error
disappears. I know the first instance is creating the Object class which
takes 0 arguments then I am overriding this with the second constructor with
my parameter. Why do I need to do this or am I missing something else.

public class ClassA
{
public ClassA() : base()
{
}

public ClassA( int Param1 )
{
}
}
Thanks

Bob
 
N

Nicholas Paldino [.NET/C# MVP]

Bob,

The reason for this is that if you do not explicitly call a constructor,
when you create your constructor, it will call the base class constructor
with no parameters. Because you define only one constructor with
parameters, there is no base class constructor that can be called from B.

Hope this helps.
 
J

Jon Skeet [C# MVP]

Bob lazarchik said:
I am trying to derive a class from an existing class with parameters in the
constructor. Does anyone know why this is happening.

Yup - see
http://www.pobox.com/~skeet/csharp/constructors.html

Basically you need to change ClassB's constructor to explicitly call
the correct constructor in ClassA:

public ClassB( int Param1 ) : base(Param1)
{
}
I get a "No overload for method 'ClassA' takes '0' arguments" compiler
error. But if i change the class A constructor to as below the error
disappears. I know the first instance is creating the Object class which
takes 0 arguments then I am overriding this with the second constructor with
my parameter.

You can't override constructors - you were overloading the constructor.
 
T

Tim Smelser

I am trying to derive a class from an existing class with parameters in the
constructor. Does anyone know why this is happening.
public class ClassA
{
public ClassA( int Param1 )
{
}
}
public class ClassB : ClassA
{
public ClassB( int Param1 )
{
}
}
I get a "No overload for method 'ClassA' takes '0' arguments" compiler
error. But if i change the class A constructor to as below the error
disappears. I know the first instance is creating the Object class which
takes 0 arguments then I am overriding this with the second constructor with
my parameter. Why do I need to do this or am I missing something else.
public class ClassA
{
public ClassA() : base()
{
}
public ClassA( int Param1 )
{
}
}
Thanks

Probably what you are trying to do is this:

public class ClassB : ClassA
{
public ClassB(int Param1) : base(Param1)
{
}
}

which should compile fine.

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top