custom superclass array?

Xojo has the Object2D superclass, with some subclasses like FigureShape, OvalShape, PixmapShape, and RectShape.

  1. I want to add some properties to the Object2D superclass, so I would have MyObject2D.
  2. I want to create an array of MyObject2D() that can hold a bunch of objects of any of those subclasses listed above

Is this idea fundamentally flawed? I’m not thinking clearly. Thanks for any help.

not sure what the question is…
but.

  • can you create subclasses of Object2D, and/or any of its subclasses?
    Yes you can, as a matter of fact that is an major internal function of my gPDF class

  • can you create an array of those subclasses?
    Yes you can. but the array would have to be either Variant or explicitly “myObject2d”… if the array is Object2D then you won’t have access to any properties you added.

I would leave the array as type Object2D and cast the elements as myObject2d when you need to access the extra properties. That would give you the greatest flexibility.

Here’s where I’m stuck:

dim p as new PixMapShape
dim o as Object2D = p // this works
dim m as myObject2D = p // this throws a compile error
dim p as new PixMapShape
dim o as Object2D = p // this works
dim m as myObject2D = o // this also throws a compile error

myObject2D has a super of Object2D

I’m missing a step or something …

And if I try to cast it, then I get an IllegalCastException at runtime. I’m stumped.

what exactly are you attempting to do…

Object2D is the base class for PixMapShape
if myObject2D is a subclass of Object2D then where Object2D could be described as the Parent of PixMapShape, then myObject2D would be an Uncle

So changes added to myObject2D won’t affect anything inheriting from Object2D

Perhaps EXTENDS might help, depending again, on just what you are attempting to do

I want to create an array of MyObject2D() that can hold objects of any of the Xojo Object2D subclasses (FigureShape, OvalShape, PixmapShape, and so on). So far this doesn’t seem possible. I either get a compile error telling me the types are mismatched, or I get an illegal cast runtime exception.

dim p as new PixMapShape
dim m as myObject2D = p // throws a Type Mismatch compile error
dim p as new PixMapShape
dim m as myObject2D = myObject2D(p) // throws an IllegalCastException at runtime

Is the ORDER they exist in your “array” important?
if not , use a dictionary instead
or a variant (yuck) array

		dim a() as Variant
		a.append new PixmapShape
		a.append new FigureShape

you will however need to know what they are and cast them when you retrieve them

OR… change you thinking about what myObject2d should be
Make it a class with properties

  • objectType as String = “PixMapShape”,'FigureShape" etc
  • objectData as Variant

then custom methods to manage the input and output of the objects

also look a the ISA command

So you want your changes to Object2D to propagate to all the other classes - pixmapshape, figureshape, etc.? That doesn’t work. It would be better in that case to create a “wrapper” class that has as one of it’s properties an Object2D, along with the additional properties you want to add to Object2D. Then you can assign a pixmapshape to that object2d property.

Thanks, Tim. I had forgotten, that’s how I did this before.