Access subclass properties from super class

Hey guys,

I have been stuck on finding a way to access a property/method of a subclass when I deal with an instance of a subclass which is initially declared as an instance of the superclass. Sounds weird? - You are right! But I just cannot figure out how to avoid getting in this trap without creating Spagetti code.

This is a simplified example:
superclass: Vehicle
1st subclass: Car (property: spoiler_yes_or_no)
2nd subclass: Truck (property: number_of_trailers)

I collected all vehicles in an Array of Vehicles. I want to iterate through the Array and add either the property “spoiler_yes_or_no” or the property “number_of_trailers” to a new array - depending on the subclass.

This is the code:

Dim vehicles() As Vehicle
Dim properties() As String
// ...array is remotely filled with instances of "Car" and "Truck"

For v As Vehicle in vehicles
   Dim type_name As String = Introspection.GetType(v).Name
   Select Case type_name
   Case "Car"
       properties.Append(v.spoiler_yes_or_no) //does not work
   Case "Truck"
      properties.Append(v.number_of_trailers) //does not work
  End Select 

Next 

==> since v is declared as instance of class Vehicle, I cannot access the properties of Car or Truck.

Can I “Redim” v to be a Car or Truck to access the property? Can I create a new Truck/Car instance and import the properties of v? Right now I see the only solution in creating two separate Arrays for Trucks and Cars, which would be quite messy in the real world.

Thanks for your help!

Ben

It’s called “Casting”. Use the class name as a function of sorts.

Properties.Append (car(v).spoiler_yes_or_no)

isA is your friend :slight_smile:

if cVechicle isA truck then MsgBox "trailers: " + str( truck( cVechicle ).numberOfTrailers ) End if

So why not just grab the instance and then ask it what properties it does have and what their values are … you’re already using introspection (which I personally might have avoided altogether but thats a different design issue)

Your code basically should know (or care) if the thing ISA car or truck etc
What you DO want to know is “I know you’re a vehicle do you have a spoiler property or not and whats its value”
So ask it

You CAN do that with introspection or simply by designing your classes so you can ask this question (what you’d do without introspection)

(This should work but I wrote it here inline soooooo … no guarantees)

		Dim vehicles() As Vehicle
		dim c as new car
		c.HasASpoiler = false
		vehicles.append c
		dim t as new truck
		t.NumberOfTrailers = 2
		vehicles.append t
		c = new car
		c.HasASpoiler = true
		vehicles.append c
		t = new truck
		t.NumberOfTrailers = 1
		vehicles.append t
		
		Dim properties() As String
		
		
		For each v As Vehicle in vehicles
				
				dim vInfo as Introspection.TypeInfo = Introspection.GetType(v)
				
				Dim type_name As String = vInfo.Name
				  
				for each p as Introspection.PropertyInfo in vInfo.GetProperties
						
						if properties.IndexOf(p.Name + "(" + p.PropertyType.Name + ")") < 0 then
								 properties.append p.Name + "(" + p.PropertyType.Name + ")"
						end if
						
				next
		next
		
		break

OR you design “Vehicle” in a way that each subclass can overload “HasAPropertyNamed” and they look it up (using introspection or not)