Mohsen
Mohsen

Reputation: 247

How to access nested class from instance of a class?

partial class Employee
{
    protected string empName;

    protected int empID = new int();

    protected float currPay;

    protected static int empAge;

    protected string empSNN;

   // these are nested classes

    public class BenefitPackage
    {
        // I want to access this class

        public enum BenefitPackageLevel
        {
            standard,Gold,Platinum
        }
        public double ComputePayDeduction()
        {
            return 125.0;
        }
    }

I'm trying to access the BenefitPackageLevel class through an instance of employees class like:

Employee emp= new Employee() 
var benefitpackage= emp.BenefitPackage.BenefitPackageLevel.standard;

but why while I didn't define BenefitPackage as a static member, I just can access it through the class level like:

Employee.BenefitPackage.BenefitPackageLevel.standard

Is it possible that nested classes are static by default?

Upvotes: 5

Views: 12292

Answers (3)

CodingYoshi
CodingYoshi

Reputation: 26989

You have a couple of issues with your code. First you need to do this change to your BenefitPackage class:

public class BenefitPackage
{
   // Your code...

   public BenefitPackageLevel Level { get; set; }
}

Then you need to do make this change to your Employee class and add the following property:

partial class Employee
{
   // Your code...

   public Employee()
   {
      this.EmoloyeeBenefitPackage = new BenefitPackage();
   }
   public BenefitPackage EmployeeBenefitPackage { get; set; }
}

Now you can do this:

var employee = new Employee();
employee.EmoloyeeBenefitPackage.Level = BenefitPackageLevel.Gold;
var level = employee.EmployeeBenefitPackage.Level;

Upvotes: 3

John Alexiou
John Alexiou

Reputation: 29244

I think you are confused. Because a type is defined within another type, it doesn't mean the outside type contains any instance information unless a field is explicitly defined.

For Example:

public class OutsideClass
{
    // Define field or properties to store nested types
    public Select Pick { get; set; }   
    public NestedClass Link { get; set; }
    // Type definitions
    public class NestedClass
    {
    }
    public enum Select
    {
        None,
        One,
        Many,
        All
    }
}

class Program
{
    static void Main(string[] args)
    {
        OutsideClass a = new OutsideClass();
        a.Pick=OutsideClass.Select.Many;
        a.Link=new OutsideClass.NestedClass();
    }
}

Upvotes: 1

Hamid Pourjam
Hamid Pourjam

Reputation: 20744

You are not accessing it as an static member. You are accessing standard through its outer types. When you declare a nested type its scope is limited to the outer type so you must access it through its outer type.

For example if you want to create an instance of BenefitPackage you should do it like this:

var benefitPackage = new Employee.BenefitPackage();

So when you want to access standard as a value of BenefitPackageLevel enumeration you must use it like this:

var temp = Employee.BenefitPackage.BenefitPackageLevel.standard;

Nested types are inaccessible to external types unless you made them public.

Keep in mind that when you create an instance of an outer type it does not create an instance of its inner types.

Upvotes: 4

Related Questions