-1

I have a class employee in which I have a static function with count number employee and and count to type of employee(e.g teacher ,assistant teacher,personal assistant etc) .For this purpose I have static class inside with I have a static count for number of employee and inside each subclass I want to access base class static method.

class Employee{
    private static int emp;
    //...code

    static void IncreaseEmployeeCount()
    {
        emp=emp+1;
    }

}

class Teacher : Employee{
    private static int tchr;

    //...code

    static void IncreaseTeacherCount()
    {
        tchr = tchr + 1;
    }
}

How can I access base class static method using child class. It tried using following but fails with compile time error:

Teacher teacher = new Teacher();
teacher.IncreaseEmployeeCount();

'Employee.IncreaseEmployeeCount()' is inaccessible due to its protection level

Adding public access level still gives an error:

Member 'Employee.IncreaseEmployeeCount()' cannot be accessed with an instance reference; qualify it with a type name instead

PaulF
  • 6,673
  • 2
  • 18
  • 29
  • 2
    Why did you add the `c++` tag to this question? – nbro Jul 29 '15 at 16:04
  • you should try super.employeeCount() instead of tch. tch is an instance and not the class it self. employeeCount() is a function on the class level and not on the instance level. – Edward Ashak Jul 29 '15 at 16:10
  • @AlexeiLevenkov: Apart from returning a value from each of the static functions, the code is acceptable C#. – PaulF Jul 29 '15 at 16:28
  • @PaulF I've got confused by all lower case... Indeed it is - I've updated sample to match default C# guidelines and be more compile-able - should be a bit better question (also I can't close this anymore) – Alexei Levenkov Jul 29 '15 at 16:34

2 Answers2

3

It should be Employee.IncreaseEmployeeCount() and Teacher.IncreaseTeacherCount(). Remember these are static methods, so they are not bound to the instance. You just call them via ClassName.StaticMethod()

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Yahya
  • 3,386
  • 3
  • 22
  • 40
0

As a static method, you cannot access it through an instance of the class. You do it like this

Teacher.IncreaseEmployeeCount();

Notes original sample had other compile errors:

  • Base class' method needs to declared as public.
  • Align return a value with body of the method as it is declared as int and should be void or have return {something};.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
PaulF
  • 6,673
  • 2
  • 18
  • 29
  • This will not work. Static methods are not inherited by child classes, they are specific/bound to a class, and not to instances. Therefore, one must call it like `employee.employeeCount()` – Matias Cicero Jul 29 '15 at 16:13
  • In c# they are inherited - try it & see. You may get a Resharper warning "Access to a static member of a type via a derived type" but that is a warning not a compiler error. – PaulF Jul 29 '15 at 16:15
  • You're right, it actually works, I'll give you that. However, this behavior is just a shortcut provided by the compiler. Syntax sugar. `teacher.employeeCount()` will just compile to `employee.employeeCount()`. As I said, `teacher` does not have static member `employeeCount()`, because static members are not inherited. – Matias Cicero Jul 29 '15 at 16:26
  • UsuallyNot best practice I agree - but Win Forms controls use inherited static properties & methods & it seems natural to invoke them from the derived class rather than the base Control class. – PaulF Jul 29 '15 at 16:36