Home > Net >  Subclass inside the subclass C# [closed]
Subclass inside the subclass C# [closed]

Time:09-28

I am having trouble accessing the subclass inside the subclass. Should I remove the employee class instead or is there a way to call the faculty class into an object. I added a subclass to inherit the variables inside the superclass person, and I needed to add a subclass INSIDE the subclass but I can't seem to access the public class faculty : employee subclass. I don't know if this makes sense, sorry.

    public class person
{
    public String name{get; set;}
    public String address{get; set;}
    public String gender{get; set;}
    public int phone{get; set;}
    public string email{get; set;}
    public int birthDate{get; set;}
    public String slg{get; set;}
    public string course{get; set;}
    public string designation{get; set;}
    public int salary{get; set;}
    public String CollegeDepartment{get; set;}
    public String SubjectLoad{get; set;}
    public String ServiceDepartment{get; set;}
}
public class student : person
{
    public student(String studentName)
    {
        name = studentName;
    }
}
public class employee : person
{
    public class faculty : employee
    {
        public faculty(String facultyName)
        {
            name = facultyName;
        }
    }
    public class staff : employee
    {
        public staff (String staffName)
        {
            name = staffName;
        }
    }
}

I can't access the faculty subclass using

    public class Program
{
    public static void Main()
    {
                
        faculty mem = new faculty("Ray");
    }
}

So basically, the problem is I can't access a subclass inside a subclass. A help will be appreciated

CodePudding user response:

You want to make sure that you have a reference to your employee class to wherever you're trying to instantiate this faculty.

For example, I created a basic console application with the classes you showed. I could not create a faculty object until I had using ConsoleApp1.Program.person.employee (where the class was stored).

using System;
using static ConsoleApp1.Program.person.employee;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            faculty bob = new faculty("wow");
        }

        public class person
        {
            public class employee : person
            {
                public class faculty : employee
                {
                    string Name { get; set; }
                    public faculty(String facultyName)
                    {
                        Name = facultyName;
                    }
                }
                public class staff : employee
                {
                    string Name { get; set; }
                    public staff(String staffName)
                    {
                        Name = staffName;
                    }
                }
            }
        }
    }
}

CodePudding user response:

Another solution is to change this...

faculty mem = new faculty("Ray");

...to this...

employee.faculty mem = new employee.faculty("Ray");
  •  Tags:  
  • c#
  • Related