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?
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.
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.
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.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With