Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why declare an instance of new class as the base type?

Why declare an instance of new class as the base type?

Scheduled Pinned Locked Moved C#
tutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AussieLew
    wrote on last edited by
    #1

    I have been working through some code from articles here, altering to suit my needs etc One particular thing puzzles me. Say DgvFilterHost inherits from DgvBaseFilterHost. Why, if a class inherits from a base class, do they declare an instance of the new class as the base type? for example....

    public DgvBaseFilterHost FilterHost {
    get {
    if (mFilterHost == null) {
    // If not provided, use the default FilterHost
    FilterHost = new DgvFilterHost();

    It seems it makes additional public properties created in DgvFilterHost inaccessible without casting. Even then I seem to have trouble accessing additional properties. Curious as to why you would not make FilterHost type DgvFilterHost in this example? Thank in advance AussieLew

    P 1 Reply Last reply
    0
    • A AussieLew

      I have been working through some code from articles here, altering to suit my needs etc One particular thing puzzles me. Say DgvFilterHost inherits from DgvBaseFilterHost. Why, if a class inherits from a base class, do they declare an instance of the new class as the base type? for example....

      public DgvBaseFilterHost FilterHost {
      get {
      if (mFilterHost == null) {
      // If not provided, use the default FilterHost
      FilterHost = new DgvFilterHost();

      It seems it makes additional public properties created in DgvFilterHost inaccessible without casting. Even then I seem to have trouble accessing additional properties. Curious as to why you would not make FilterHost type DgvFilterHost in this example? Thank in advance AussieLew

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      There are many reasons for doing this. One reason for doing this is to use Dependency Injection, where you need to refer to a type in your code, but the actual implementation you'd be working on is a derived class. This is also typical behaviour in abstract class factories.

      I'm not a stalker, I just know things. Oh by the way, you're out of milk.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups