By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

InvalidCastException when type-casting arrays

P: n/a
I have a set of business object classes built from the typed dataset tool in
Visual Studio. Since the individual objects (such as the Order object, for
example) inherit from DataRow, a common requirement is to cast an array of
DataRows to an array of Orders. But I keep getting InvalidCastExceptions
when I do this, for example:

Dim orders As Order() = CType(RowsAffected, Order())

In this example, RowsAffected returns an array of DataRows after copying
them from the ArrayList where they're collected:

Public ReadOnly Property RowsAffected() As DataRow()
Get
Dim rows() As DataRow
If m_RowsAffected.Count > 0 Then 'm_RowsAffected is an
ArrayList
ReDim rows(m_RowsAffected.Count - 1)
m_RowsAffected.CopyTo(rows)
Else
ReDim rows(-1)
End If
Return rows
End Get
End Property

I am getting this exception even when the number of rows affected is higher
than 0. When I break into the code, I can interrogate CType(RowsAffected,
Order()) in the Immediate window and get the expected result, that is:

?CType(RowsAffected, Order())
{Length=1}
(0): {MyAssembly.DataObjects.Order}

This seems a lot like a bug to me. Am I missing something?

Any insight is appreciated. Thanks.

Regards,

Jon
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.