Take the 2-minute tour ×

I have 2 different widgets that share 10 of the same members. however they have 2 stages as well. each stage has specific members that the other does not.

public class widget

   private _id as integer
   private _engravedId as string
   private _stage as WidgetStage
   private _wig as IWidget
end class

public interface IWidget
end interface

public class Twidget
   implemets IWidget

   private t as integer
   private b as double
   private d as double

end class

public class Rwidget
   implemenents IWidget

   private y as integer
   private x as double
   private b as double

end class

if i declare an Iwidget in my class then I will have to cast it to access the derived class variables. Is there a better way?

share|improve this question
If the additional variables are private, how are you going to access those even if you cast it? –  Prescott Aug 4 '12 at 1:25
add comment

1 Answer

If all of the private variables have the same meaning in the context of IWidget, then you can add properties to IWidget to retrieve them.

Otherwise, you could implement reflection to retrieve the values but unless you are doing something like mapping properties to database columns or user interface element values, that is most likely not the correct approach.

This means that casting is most likely the best way for your scenario.

share|improve this answer
add comment

Your Answer


