using the object.GetType to get the dynamic type

T

Tony Johansson

Hello!

In this code I use the GetType and it works but I just wonder if it exist
any better way to get the runtime type.

public Animal GetElement(string name, string animalType)
{
return (Animal)animals.Find(delegate(Animal item)
{
return string.Equals(item.Name, name,
StringComparison.CurrentCultureIgnoreCase) &&
string.Equals(item.GetType().ToString().Substring(item.GetType().ToString().LastIndexOf(".")
+ 1), animal, StringComparison.CurrentCultureIgnoreCase);
});
}

//Tony
 
J

Jeff Johnson

In this code I use the GetType and it works but I just wonder if it exist
any better way to get the runtime type.

public Animal GetElement(string name, string animalType)
{
return (Animal)animals.Find(delegate(Animal item)
{
return string.Equals(item.Name, name,
StringComparison.CurrentCultureIgnoreCase) &&

string.Equals(item.GetType().ToString().Substring(item.GetType().ToString().LastIndexOf(".")
+ 1), animal, StringComparison.CurrentCultureIgnoreCase);
});
}

First, I assume the "animal" near the end of the code is supposed to say
"animalType," i.e., the second parameter to the function. (This is why we
always tell people to COPY AND PASTE existing code and not hand-type it into
a post!)

Since you'll obviously be keeping a list of type names in your current
scenario, why not instead keep a list of types, pass the type itself, and
then see if item.GetType().Equals(animalType)?
 
A

Arne Vajhøj

In this code I use the GetType and it works but I just wonder if it exist
any better way to get the runtime type.

public Animal GetElement(string name, string animalType)
{
return (Animal)animals.Find(delegate(Animal item)
{
return string.Equals(item.Name, name,
StringComparison.CurrentCultureIgnoreCase)&&
string.Equals(item.GetType().ToString().Substring(item.GetType().ToString().LastIndexOf(".")
+ 1), animal, StringComparison.CurrentCultureIgnoreCase);
});
}

No better way than GetType.

But maybe the code can still be simplified.

string.Equals(item.GetType().ToString().Substring(item.GetType().ToString().LastIndexOf(".")
+ 1), animal, StringComparison.CurrentCultureIgnoreCase)

should be identical to:

string.Equals(item.GetType().Name, animal,
StringComparison.CurrentCultureIgnoreCase)

if I have understood the logic correctly.

Now I don't know what animal variable is, but I suspect that an
even better solution would be:

string.Equals(item.Xxx, animal, StringComparison.CurrentCultureIgnoreCase)

where Xxx is a property that contains what you want to compare.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
No better way than GetType.

But maybe the code can still be simplified.

string.Equals(item.GetType().ToString().Substring(item.GetType().ToString().LastIndexOf(".")

should be identical to:

string.Equals(item.GetType().Name, animal,
StringComparison.CurrentCultureIgnoreCase)

if I have understood the logic correctly.

Now I don't know what animal variable is, but I suspect that an
even better solution would be:

string.Equals(item.Xxx, animal, StringComparison.CurrentCultureIgnoreCase)

where Xxx is a property that contains what you want to compare.

Arne

Yes I found the Name property but haven't had the time to post this info.
Yes the animal qualifier is a typo it should be animalType
The solution that I have done now is much better when I removed the
unnessessary data from the base class Animal. I have also removed almost all
data from the AnimalManager class except
the collection class

//Tony
 
A

Arne Vajhøj

Yes I found the Name property but haven't had the time to post this info.
Yes the animal qualifier is a typo it should be animalType
The solution that I have done now is much better when I removed the
unnessessary data from the base class Animal. I have also removed almost all
data from the AnimalManager class except
the collection class

I still think that a property is better than relying on the class name.

One of the bad things that can happen is that you use the IDE to rename
the class. Modern IDE's are pretty smart and they can rename all the
references to the class name. But they do not change a string literal.

Arne
 
I

ib.dangelmeyr

Hello!

In this code I use the GetType and it works but I just wonder if it exist
any better way to get the runtime type.

public Animal GetElement(string name, string animalType)
      {
         return (Animal)animals.Find(delegate(Animal item)
         {
            return string.Equals(item.Name, name,
StringComparison.CurrentCultureIgnoreCase) &&
                   string.Equals(item.GetType().ToString().Substring(item.GetType().ToString().LastIndexOf(".")
+ 1), animal, StringComparison.CurrentCultureIgnoreCase);
         });
      }

//Tony

Good OO design would not require the need of comparing object types.
Instead there would be something like a virtual function
"AreYouAShark()".
 
I

ib.dangelmeyr

Ugh.  No, there would not be.  Why should every instance of Animal be
saddled with a method that simply returns whether the instance is a
Shark or not?

In the Animal class, there would virtual members corresponding to things
common to _all_ Animals.  The Shark class would override it with
shark-appropriate behaviors.  But having a Shark-specific member in the
base class doesn't make sense.

Having a base-class method called "AreYouAShark" gets out of hand when
you've got thousands or tens of thousands, or hundreds of thousands (I
really don't know how far Tony plans to go with this animal-modeling
project :) ), and you need a base-class method to check for each and
every animal type.

If all you need is to know the specific type, the best approach is
something like the "is" or "as" operators.  For polymorphic behaviors
common to all classes inheriting a given base class, a virtual member
declared in the base class is appropriate.

Checking the type name itself is poor, and I agree with Arne that having
a specific property (presumably virtual) that returns a well-defined
string is better.  But IMHO only barely.  Instead, if there's some
string input that needs to be compared with instances of objects, that
string should map to an actual Type instance, and then reflection (e.g.
the Type.IsInstanceOfType()) can be used to detect instances that match
that type.

Pete

You're right ... it was a very poor example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top