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. Checking an objects inheritance

Checking an objects inheritance

Scheduled Pinned Locked Moved C#
oopquestionlearning
3 Posts 3 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.
  • C Offline
    C Offline
    cnurse
    wrote on last edited by
    #1

    In the following code pTable is of type CustomerTable, which inherits from DataTable. How can I change the "if" statement to check that pTable is derived from DataTable as the statement currently tests false, because the types are different. I thought I would cast ptable, but the of course that would throw an exception if that cast is invalid. if (pTable != typeof(DataTable)) { throw new EwGeneralException(lStk, EwExceptionID.DdlNotDataTable); } Many thanks for reading. Nursey

    J H 2 Replies Last reply
    0
    • C cnurse

      In the following code pTable is of type CustomerTable, which inherits from DataTable. How can I change the "if" statement to check that pTable is derived from DataTable as the statement currently tests false, because the types are different. I thought I would cast ptable, but the of course that would throw an exception if that cast is invalid. if (pTable != typeof(DataTable)) { throw new EwGeneralException(lStk, EwExceptionID.DdlNotDataTable); } Many thanks for reading. Nursey

      J Offline
      J Offline
      jqd2001
      wrote on last edited by
      #2

      Can you try if (typeof(pTable).BaseType !=typeof(DataTable)) .... James

      1 Reply Last reply
      0
      • C cnurse

        In the following code pTable is of type CustomerTable, which inherits from DataTable. How can I change the "if" statement to check that pTable is derived from DataTable as the statement currently tests false, because the types are different. I thought I would cast ptable, but the of course that would throw an exception if that cast is invalid. if (pTable != typeof(DataTable)) { throw new EwGeneralException(lStk, EwExceptionID.DdlNotDataTable); } Many thanks for reading. Nursey

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        You're comparing apples and oranges, there. pTable is an instance, and the typeof operator will always return a Type, so the two would never match unless pTable was a variable which held a Type. If you want to check is something is an instance of a particular type, simply use the is operator:

        if (pTable is DataTable)
        {
        // ...
        }

        You can also use something like this, although it requires more code and more instructions (when compiled to IL):

        if (pTable != null)
        {
        if (pTable.GetType() == typeof(DataTable))
        {
        // ...
        }
        }

        There's other ways of doing this, but you don't have to take inheritance into account. If CustomerTable inherits form DataTable, then any instance is a type of CustomerTable, DataTable, MarshalByValueComponent, and - as with everything in .NET - Object. It's also an instance of any interfaces that any classes up the inheritence hierarchy implement. This isn't true going down the hierarchy, though. For example, if you instantiate a new instance of a DataTable, then it is not an instance of a CustomerTable. Also keep in mind that it's not the declaration that counts, its the definition. For example:

        object o = new DataTable("Example");

        While o is declared as an object, it's still an instance of a DataTable. Calling o.GetType will return System.Data.DataTable. This is polymorphism.

        Microsoft MVP, Visual C# My Articles

        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