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