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. Looking for a base class with typeof?

Looking for a base class with typeof?

Scheduled Pinned Locked Moved C#
questiondatabasetutorial
5 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.
  • A Offline
    A Offline
    Andrew Stampor
    wrote on last edited by
    #1

    I have a case where several objects may trigger an event that is handled in one place. I use the sender object type to determine whether I should perform some task on the object. However, how can I catch the condition where an object inherits from the type of object I am interested in? I'd like to include that object as well. Here is an example of what I am doing: if (sender.GetType() == typeof(CMyClass) ) { //Do Something } If something is derived from CMyClass, I'd like it to go through the "Do Something" code. How can I check for it?

    R G 2 Replies Last reply
    0
    • A Andrew Stampor

      I have a case where several objects may trigger an event that is handled in one place. I use the sender object type to determine whether I should perform some task on the object. However, how can I catch the condition where an object inherits from the type of object I am interested in? I'd like to include that object as well. Here is an example of what I am doing: if (sender.GetType() == typeof(CMyClass) ) { //Do Something } If something is derived from CMyClass, I'd like it to go through the "Do Something" code. How can I check for it?

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      Just test if you can cast it to the other class/interface:

      if (sender as CMyClass != null)
      {
      //Do Something
      }

      A 1 Reply Last reply
      0
      • A Andrew Stampor

        I have a case where several objects may trigger an event that is handled in one place. I use the sender object type to determine whether I should perform some task on the object. However, how can I catch the condition where an object inherits from the type of object I am interested in? I'd like to include that object as well. Here is an example of what I am doing: if (sender.GetType() == typeof(CMyClass) ) { //Do Something } If something is derived from CMyClass, I'd like it to go through the "Do Something" code. How can I check for it?

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Use the is keyword: if (sender is CMyClass) { //Do Something } --- b { font-weight: normal; }

        A 1 Reply Last reply
        0
        • R Robert Rohde

          Just test if you can cast it to the other class/interface:

          if (sender as CMyClass != null)
          {
          //Do Something
          }

          A Offline
          A Offline
          Andrew Stampor
          wrote on last edited by
          #4

          Ah, thanks! That is what I wanted. In case anyone else is interested, another way to handle this issue (or get around it) is by associating your base class with an interface. All inherited classes are also of the same interface so I was able to do something like this:

          if (sender is ISomeInterface)
          {
          // Do Something
          }

          1 Reply Last reply
          0
          • G Guffa

            Use the is keyword: if (sender is CMyClass) { //Do Something } --- b { font-weight: normal; }

            A Offline
            A Offline
            Andrew Stampor
            wrote on last edited by
            #5

            Ah! This is what I want. Thanks.

            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