Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access nested class from instance of a class?

Tags:

c#

.net

nested

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?

like image 517
Mohsen Avatar asked Mar 18 '17 14:03

Mohsen


People also ask

How do I access nested class?

They are accessed using the enclosing class name. For example, to create an object for the static nested class, use this syntax: OuterClass. StaticNestedClass nestedObject = new OuterClass.

Can nested class be accessed outside?

Unlike the non-static nested classes, the static nested class cannot directly access the instance variables or methods of the outer class. They can access them by referring to an object of a class.

How do you reference a nested class method?

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.

What is the scope of class nested inside another class?

A class can be declared within the scope of another class. Such a class is called a "nested class." Nested classes are considered to be within the scope of the enclosing class and are available for use within that scope.


2 Answers

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.

like image 93
Hamid Pourjam Avatar answered Sep 20 '22 08:09

Hamid Pourjam


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;
like image 45
CodingYoshi Avatar answered Sep 20 '22 08:09

CodingYoshi