Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding a Nested Class in derived class C#

Tags:

c#

oop

I want to have a base class with a nested class inside it - and expand the nested class in the child class: From some reason - I can't access the Nested Derived Class NEW (added) properties:

public class BaseClass
{
    public NestedClass nestedClass {get; set;}
    public BaseClass()
    {
       nestedClass = new NestedClass();
    }
    public int baseproperty {get; set;}
    public  class NestedClass
    {
        public int NestedProperty {get; set;}
    }
}

public class Derived : BaseClass
{
    public class NestedClass
    {
          public DeeperNestedClass deeper {get; set;}
          public NestedClass()
          {
              deeper = new DeeperNestedClass();
          }
          public class DeeperNestedClass
          {
               public int VeryDeepPropery {get; set;}
          }


    }
}

Derived MyDerived = new Derived();
MyDerived.nestedClass.deeper   <----- Doesn't recognized !

* edit - fixed the missing "class" in base class

like image 850
Dani Avatar asked Feb 12 '23 01:02

Dani


1 Answers

You basically have three problems:

1) You actually have to derive Derived.NestedClass from BaseClass.NestedClass.

2) Your property BaseClass.nestedClass will always have the type of BaseClass.NestedClass so you need to cast it to access the deeper property.

3) You have to make sure that the property BaseClass.nestedClass actually hold an object of the type Derived.NestedClass when accessed in Derived.

public class BaseClass {
   public NestedClass nestedClass { get; set; }

   public BaseClass() {
      nestedClass = new NestedClass();
   }

   public int baseproperty { get; set; }

   public class NestedClass {
      public int NestedProperty { get; set; }
   }
}

public class Derived : BaseClass {
   public Derived() {
      nestedClass = new NestedClass();
   }

   public class NestedClass : BaseClass.NestedClass {
      public DeeperNestedClass deeper { get; set; }

      public NestedClass() {
         deeper = new DeeperNestedClass();
      }

      public class DeeperNestedClass {
         public int VeryDeepPropery { get; set; }
      }
   }
}

class Program {
   static void Main(string[] args) {
      Derived derived = new Derived();
      BaseClass.NestedClass nestedBase = derived.nestedClass;
      Derived.NestedClass nestedDerived = (Derived.NestedClass)nestedBase;
      Derived.NestedClass.DeeperNestedClass deeper = nestedDerived.deeper;
      int property = deeper.VeryDeepPropery;
   }
}

Edit after the discussion in the comments: Maybe something simple like this will work for you?

public class OuterClass {
   public OuterClass() {
      nestedClass = new NestedClass();
   }

   public int baseproperty { get; set; }
   public NestedClass nestedClass { get; set; }
}

public class NestedClass {
   public NestedClass() {
      deeper = new DeeperNestedClass();
   }
   public int NestedProperty { get; set; }
   public DeeperNestedClass deeper { get; set; }
}

public class DeeperNestedClass {
   public int VeryDeepPropery { get; set; }
}

class Program {
   static void Main(string[] args) {
      OuterClass outer = new OuterClass();
      int property = outer.nestedClass.deeper.VeryDeepPropery;
   }
}
like image 94
cremor Avatar answered Feb 15 '23 11:02

cremor