Derived class constructor errors
Trying to explain this very concisely
- I have an abstract base class, and 2 derived classes.
- The base class has a constructor that takes in a single string argum开发者_StackOverflow中文版ent and assigns it to a field
- The derived classes constructors call the base class constructor and each take a single string argument.
- These are part of a class library project that is referenced in the main solution.
- In the main, while I attempt to create an instance of the class, passing in a single argument (as should be), I run across this error:
library.class
does not contain a constructor that takes a single argument.
If I create an object without a parameter the following error line is created:
library.class.constructor(string)
is inaccessible due to it's protection level.
I am not able to find a solution to this problem, I am providing my essential code below, if anyone can take a look and gauge what mistake is happening here, that would be really awesome:
The base class:
abstract class EmployeeBase
{
private string empNumber;
public EmployeeBase(string CurrentEmployeeNumber) { empNumber = CurrentEmployeeNumber;}
......
The derived class:
class EmployeeExempt : EmployeeBase
{
public EmployeeExempt(string CurrentEmployeeNumber) : base(CurrentEmployeeNumber)
{ }
........
The main method:
EmployeeExempt emp1 = new EmployeeExempt("1000");
This I believe is the core of my program, hopefully somebody can spot out the mistake that is happening here
You should explicitly declare both your base class and your derived class as public
. The default accessibility for classes is internal
so both of these classes are inaccessible from your main method. They are only accessible from within your class library project.
You should be getting three error messages:
Test.cs(7,9): error CS0122: 'EmployeeExempt' is inaccessible due to its
protection level
Test.cs(7,35): error CS0122: 'EmployeeExempt' is inaccessible due to its
protection level
Test.cs(7,31): error CS1729: 'EmployeeExempt' does not contain a constructor
that takes 1 arguments
The first two should make the problem pretty clear - as the other answers have said, EmployeeBase and EmployeeExempt are both internal classes (the default accessibility for non-nested types), which means they're only accessible within the same assembly. The class containing the Main method is in a different assembly, so you don't have access to them.
Just make them public.
I'll admit that the last error message is slightly misleading, but you should have looked at all the error messages rather than just that one - the first two should have given you enough hints about what was going on.
The only problem I see, is that EmployeeExempt
is internal
and not public
, but that doesn't match the error message...
I assume, you haven't shown us all relevant code, or the code really is slightly different from what you showed.
Both of your classes are being declared private. Prefix the class names with public or if they are intended to be in the same assembly perhaps internal.
The string in the base class EmployeeBase is private . Try making it public .
精彩评论