type casting mystery

  • Thread starter Thread starter Jon the Blind
  • Start date Start date
J

Jon the Blind

I would greatly appreciate if someone could shed some light on this.

In a class inherited from CollectionBase, I have a function something like
this:

Public Function ToArray() As Widget()
Return CType(MyBase.InnerList.ToArray, Widget())
End Function

This is meant to return the contents of the collection as an array (!). When
running the code, I get an InvalidCastException here, but if I break into
the code and run just

?CType(MyBase.InnerList.ToArray, Widget())

in the Immediate Window, I don't get the exception.

Runnning this in the Immediate Window gets an interesting result:

?CType(MyBase.InnerList.ToArray, Widget()).GetType.ToString
"System.Object[]"

Assuming I'm not putting too much faith in the Immediate Window replicating
the behavior of the runtime, is this an error with the CType function? Or am
I missing something fundamental?

Any insight is appreciated.

Regards,

Jon
 
You are trying to cast down in example 1 think. In example two, you will
get the base class toString function. Because you asked for it.

If you post more code, it might be easier to help.

OHM
 
Back
Top