Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object types in an array

 
Mike Bursill
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short version of my question (Using "How my Dog learned Polymorphism article" as the example):

If I had the d object (the Dog class inherited from the Animal class) with the added method Bark() stored into an array of Animals[] how could I ever cycle through all the objects in the array, figure out that one of them is Dog and access the method Bark()?

The longer more detailed version of my question:

Just for fun, I'm developing a few C# programs (the problem isnt C# specific, just OO specific). One of these programs is to skin C# winforms. I have most if it completed, including the reading of the files which make the skin from a compressed zip, and also the parsing of an XML file defining all the elements and there locations. However, I have run into one small OO glitch.

I have 3 class's:

class SkinFiles
{
public String Type = "";
public String ID = "";
public String FileName = "";
}

class ImageFile:SkinFiles
{
public Color ColorKey;
}

class SoundFile:SkinFiles
{
//not implemented yet
}


As I parse the XML file (defining the Skin), it tells me the Type/ID and Name of a file I am going to need form the ZIP (which holds all the skins stuff, pic's sounds' etc.). I create a new instance of the ImageFile or SoundFile object to hold this info depending on what the file type is (Also defined by the XML). I then store the object into a collection.

I am using a collection, so that I can reference the objects via a string (the name of the file). I do this so when I cycle through all the files of the ZIP, I can tell instantly the ID to be assigned to the incoming file, and possibly a ColorKey or other file specific info to that type of file.

Since the collection stores the class as type "Object", when I need to reference it, I must type cast it back to whatever type it is.

My problem is, how do I know what that type is? I can type caste it back to SkinFiles and get access to those public instance variables, but I loose the ability to read the ColorKey variable if the saved object so happened to be a ImageFile object.

Thanks!

-Mike.
 
Jacquie Barker
author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike, there are several ways to "interrogate" objects in a collection to find out their types, as follows:
if (x.getClass().getName().equals("Dog")) {
Dog d = (Dog) x;
d.bark();
}
or
if (x instanceof Dog) { ... }
Hope this helps!

Originally posted by Mike Bursill:
The short version of my question (Using "How my Dog learned Polymorphism article" as the example):

If I had the d object (the Dog class inherited from the Animal class) with the added method Bark() stored into an array of Animals[] how could I ever cycle through all the objects in the array, figure out that one of them is Dog and access the method Bark()?

The longer more detailed version of my question:

Just for fun, I'm developing a few C# programs (the problem isnt C# specific, just OO specific). One of these programs is to skin C# winforms. I have most if it completed, including the reading of the files which make the skin from a compressed zip, and also the parsing of an XML file defining all the elements and there locations. However, I have run into one small OO glitch.

I have 3 class's:

class SkinFiles
{
public String Type = "";
public String ID = "";
public String FileName = "";
}

class ImageFile:SkinFiles
{
public Color ColorKey;
}

class SoundFile:SkinFiles
{
//not implemented yet
}


As I parse the XML file (defining the Skin), it tells me the Type/ID and Name of a file I am going to need form the ZIP (which holds all the skins stuff, pic's sounds' etc.). I create a new instance of the ImageFile or SoundFile object to hold this info depending on what the file type is (Also defined by the XML). I then store the object into a collection.

I am using a collection, so that I can reference the objects via a string (the name of the file). I do this so when I cycle through all the files of the ZIP, I can tell instantly the ID to be assigned to the incoming file, and possibly a ColorKey or other file specific info to that type of file.

Since the collection stores the class as type "Object", when I need to reference it, I must type cast it back to whatever type it is.

My problem is, how do I know what that type is? I can type caste it back to SkinFiles and get access to those public instance variables, but I loose the ability to read the ColorKey variable if the saved object so happened to be a ImageFile object.

Thanks!

-Mike.

[ October 15, 2003: Message edited by: Jacquie Barker ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I had the d object (the Dog class inherited from the Animal class) with the added method Bark() stored into an array of Animals[] how could I ever cycle through all the objects in the array, figure out that one of them is Dog and access the method Bark()?
Let me point out that the point of using Polymorphism in many situations is so that you would never do such a thing. In more polymorphic conditions, you'd perhaps have a collection of animals that can all speak in one way or another - dogs bark, birds chirp, cows moo, etc. Then, you'd iterate over the collection and just tell each animal to speak - you would't care what type the animal were. The dogs would bark, the birds would chirp, the cows would moo, and all would be right with the world. (OK, so maybe that last part is a bit much to ask of polymorphism.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic