Need some help. This example if from the Language Guide, (PropertyInfo.Value).

I have modified it slightly to set the ObjArray= ObjArray but that fails:

[code]Dim s() As Object
s = pi.Value(t)

pi.Value(t)=s //Here’s my problem


TestClass looks like this:

Property TestArray() as string Property ObjArray() as TestClass

Some experimenting has be believing that I need to cast the s() As Object to s() As TestClass.
But when I do that the type of ObjArray is TestClass() when it normally is Object() (When doing introspection of the property ObjArray).

My question probably is: How do I set a property to an array of classes with introspection?

[code]Dim t As New TestClass

t.TestArray.Append(“Some more text”)

t.ObjArray.Append new TestClass
t.ObjArray.Append new TestClass

Dim ti As Introspection.TypeInfo
ti = Introspection.GetType(t)

Dim p() As Introspection.PropertyInfo
p = ti.GetProperties

For Each pi As Introspection.PropertyInfo In p
Dim pt As Introspection.TypeInfo
pt = pi.PropertyType

Select Case pi.Name
Case "TestArray"
  If pt.IsArray Then
    If pi.Value(t).ArrayElementType = 8 Then // String
      Dim s() As string
      s = pi.Value(t)
      MsgBox("Array = (" + Join(s, ",") + ")")
    End If
  End If
Case "ObjArray"
  If pt.IsArray Then
    Dim s() As Object
    s = pi.Value(t)
    pi.Value(t)=s  //Here's my problem
  end if
End Select


The problem is upcasting vs. downcasting. You can downcast an object into its super class but not the other way around.

In your case, since you know the type of array, you can do this:

dim s() as TestClass = pi.Value( t )
pi.Value( t ) = s


dim s() as object = pi.Value( t )
redim s( -1 )
s.Append new TestClass
s.Append new TestClass
// No need to assign it back since it's the array that
// was stored in the property originally

I think your terms are backwards but …

from sub to super is “upcast” (if you imagine a tree that is rooted at the top)
from super into sub is “downcast” (again if you imagine the top of the hierarchy is at the top)

ie/ if you imagine the inheritance tree is like they are usually drawn with the most derived types at the bottom

         |        |            |
     subtype1 subtype2    subtype3
     |                         |
  +--+-----------+             +-------+----------+
  |              |                     |          |
subsubtype1   subsubtype2          subsubtype3   subsubtype4

Kem and Norman, thanks guys.

Kem, you alternative 1 does not seem to work properly for me (just tested it briefly) but the second alternative:

dim s() as object = pi.Value( t ) redim s( -1 ) s.Append new TestClass s.Append new TestClass

worked perfect in the code where I needed it :slight_smile:

You saved my christmas!

Now that you mention it, I think that’s how I do it in my code too. Glad it worked out.

I’m guessing this is a bug or limitation with Variant itself, which is what the classic framework uses for its introspection API.

Here’s a much simpler example of Variant’s behavior:

		Dim dictionaryArray() As Dictionary
		Dim v As Variant = dictionaryArray
		Dim objArray() As Object = v