Determine the type of an object

deejross

Member
Joined
May 10, 2006
Messages
8
Programming Experience
3-5
I've been trying to figure this out for a while and would hope someone can help me. There's no real way to ask, so I have to ask with an example. Let's say I want to make a control that acts kind of like the form designer. At run-time, you can drag, pre-coded objects onto the control. Let's say I've pre-coded a few controls (GraphicsLabel GraphicsShape, etc.), and they all inherit my basic GraphicsObject class. I would need to keep those objects in a list somewhere, so I make an arraylist that holds all the objects drawn. Now, I want to enumerate through that list, so I do this:
VB.NET:
For each obj as GraphicsObject in objList
...
Next
Which is a problem because I would need to interact with each object differently based on its type. So how would I tell if the current obj in the enumeration was a GraphicsLabel, or any other type of object that inherits GraphicsObject? This is kind of tough to explain and I hope it makes sense. Thanks in advance for your help.
 
So, doing a For Each as a GraphicsObject, you can also evaluate if it's another type? I thought there was no way to tell what kind of object something was once it was added to an arraylist because it gets added as a System.Object.

If that's not the case, then technically I could do a For Each as System.Object, then do a If TypeOf obj is GraphicsLabel or TypeOf obj is GraphicsShape Then...

Am I correct?
 
Hi :)
Im not sure but you can try this
VB.NET:
Select Case TypeOf passedObject
 
Case Is GraphicShape.GetType
 
     ' What Do you want to do
 
Case Is GraphicLabel.GetType
 
' ......etc

Another Way:
if you want as an example :to get the area of each Shape
You Can Make an "interface " which has a function called :
GETArea()
and implements the interface in each Class that inherits GraphicObject Class

then you can call the function whatever the Object Be ??

VB.NET:
Interface Graphic
Sub GetArea()
End Interface
 
 
'now in each control Do like this
 
Class LabelGraphic
Implements Graphic
 
Sub GetArea() implements Graphic.GetArea
  ' type some code here
 
End Sub
 
'now outside you can pass any kind of object that implements Graphic Interface
 
Sub DrawControl(object as Graphic)
Graphic.GetArea()
 
 
End Sub

Sorry for mistake but i hope this will help you;)
 
Back
Top