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. object type [modified]

object type [modified]

Scheduled Pinned Locked Moved C#
question
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.
  • N Offline
    N Offline
    nlowdon
    wrote on last edited by
    #1

    Hi, I am using an interface a stepping through a list for my application to perform a method. (listed below)

    foreach (IPetAnimalKeeper pettable in zooAnimals.OfType<IPetAnimalKeeper>())
    {
    pettable.pet();
    }

    Is there a way that i can then check the type of 'pettable' ? reason being i would then like to call another method but would need to know the type first. For instance if i wanted to call a 'move()' method.......i would need to know which animals 'move()' to call. I've just tried putting in a 'break point' at the end of the 'foreach' and when i hover the mouse over 'pettable' i can see it's a Cat but if i code

    pettable.

    i can only access the methods in 'pettable' not in the Cat class ? Thats the methods i'd like access to. Sorry, i know thats a bitconfusing. Thanks Neil

    modified on Friday, December 5, 2008 12:48 PM

    T L 2 Replies Last reply
    0
    • N nlowdon

      Hi, I am using an interface a stepping through a list for my application to perform a method. (listed below)

      foreach (IPetAnimalKeeper pettable in zooAnimals.OfType<IPetAnimalKeeper>())
      {
      pettable.pet();
      }

      Is there a way that i can then check the type of 'pettable' ? reason being i would then like to call another method but would need to know the type first. For instance if i wanted to call a 'move()' method.......i would need to know which animals 'move()' to call. I've just tried putting in a 'break point' at the end of the 'foreach' and when i hover the mouse over 'pettable' i can see it's a Cat but if i code

      pettable.

      i can only access the methods in 'pettable' not in the Cat class ? Thats the methods i'd like access to. Sorry, i know thats a bitconfusing. Thanks Neil

      modified on Friday, December 5, 2008 12:48 PM

      T Offline
      T Offline
      Thomas Weller 0
      wrote on last edited by
      #2

      You can use one of the following methods to do that:

      if (pettable.GetType() == typeof(myType))
      {
      ...
      }

      if (pettable.GetType().Name == "myType")
      {
      ...
      }

      if (pettable is myType)
      {
      ...
      }

      But you probably shouldn't do it that way. When you are looking for a certain method to call, you should always do so by using an interface. Avoid looking for a concrete type in your code wherever possible. This is bad coding style! It is a far better solution to create another interface for each method you want to know about, exactly like you did it with the pet() method. This is one of the main purposes of the syntactical construct interface: They mark a type to have a certain member (i.e. method/property/event). Regards Thomas

      www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
      Programmer - an organism that turns coffee into software.

      N 1 Reply Last reply
      0
      • N nlowdon

        Hi, I am using an interface a stepping through a list for my application to perform a method. (listed below)

        foreach (IPetAnimalKeeper pettable in zooAnimals.OfType<IPetAnimalKeeper>())
        {
        pettable.pet();
        }

        Is there a way that i can then check the type of 'pettable' ? reason being i would then like to call another method but would need to know the type first. For instance if i wanted to call a 'move()' method.......i would need to know which animals 'move()' to call. I've just tried putting in a 'break point' at the end of the 'foreach' and when i hover the mouse over 'pettable' i can see it's a Cat but if i code

        pettable.

        i can only access the methods in 'pettable' not in the Cat class ? Thats the methods i'd like access to. Sorry, i know thats a bitconfusing. Thanks Neil

        modified on Friday, December 5, 2008 12:48 PM

        L Offline
        L Offline
        Le centriste
        wrote on last edited by
        #3

        Instead of that, have an interface that declares the Move(), and have your animals implement the IPetAnimalKeeper, but if your animal can move, also make them implement the interface IMovingPet

        if (pettable is IMovingPet)
        {
            ((IMovingPet)pettable).Move();
        }
        

        There are other ways, probably more elegant.

        1 Reply Last reply
        0
        • T Thomas Weller 0

          You can use one of the following methods to do that:

          if (pettable.GetType() == typeof(myType))
          {
          ...
          }

          if (pettable.GetType().Name == "myType")
          {
          ...
          }

          if (pettable is myType)
          {
          ...
          }

          But you probably shouldn't do it that way. When you are looking for a certain method to call, you should always do so by using an interface. Avoid looking for a concrete type in your code wherever possible. This is bad coding style! It is a far better solution to create another interface for each method you want to know about, exactly like you did it with the pet() method. This is one of the main purposes of the syntactical construct interface: They mark a type to have a certain member (i.e. method/property/event). Regards Thomas

          www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
          Programmer - an organism that turns coffee into software.

          N Offline
          N Offline
          nlowdon
          wrote on last edited by
          #4

          Thanks Thomas, I'm new to interfaces so thats probably why i've left that way out just now (not too confident with them !). Regarding the first option you gave, i understand how that works, but still cant see how it will allow me to access the methods in the animals own class....say Cat class ? Can i cast the 'pettable' to instance of an object 'Cat' ?

          T 1 Reply Last reply
          0
          • N nlowdon

            Thanks Thomas, I'm new to interfaces so thats probably why i've left that way out just now (not too confident with them !). Regarding the first option you gave, i understand how that works, but still cant see how it will allow me to access the methods in the animals own class....say Cat class ? Can i cast the 'pettable' to instance of an object 'Cat' ?

            T Offline
            T Offline
            Thomas Weller 0
            wrote on last edited by
            #5

            nlowdon wrote:

            Can i cast the 'pettable' to instance of an object 'Cat' ?

            Exactly. Regards Thomas

            www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
            Programmer - an organism that turns coffee into software.

            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