I will bold your problem:
Code:
Private Function ReadDataClassAllFieldsToDisk(ByVal CheckObject() As Object) As Boolean
Using
Object is a bad thing. It removes the ability of the compiler to do type checking, and can help you make subtle logic errors that would otherwise be caught at compile time.
You haven't really showed enough code for me to make a good decision, but with what you've given, the function should have been declared:
Code:
Private Function ReadDataClassAllFieldsToDisk(ByVal CheckObject() As Hissdata) As Boolean
I'd love to give you advice specific to the problem, but you didn't show enough code for me to make a reasonable suggestion. In particular, you didn't show the line of code that throws the exception, which one of the two lines you needed to post.
There's really three better ways to handle this situation.
1. Generics
If your function has to process arrays of several types of objects, you may consider using generics to let the function maintain strong typing and handle several types. For example, this method prints all values of any array to the console:
Code:
Sub PrintArray(Of T)(ByVal array() As T)
For Each item As T In array
Console.WriteLine(item)
Next
End Sub
Using this is pretty straightforward:
Code:
Dim stringValues() As String = {"one", "two", "three"}
Dim intValues() As Integer = {1, 2, 3}
PrintArray(stringValues)
PrintArray(intValues)
2. Interfaces
Using interfaces is
slightly more complicated. It requires you to examine the data classes and identify the common methods and properties, then move them into an interface. The method works with the interface type. This lets it maintain strong typing without losing generic functionality.
3. Duplicate Code
If generics or interfaces don't work, I daresay this is an instance where it'd be better to just put a
ReadAllFieldsToDisk method on each of your data classes. If it's serialization code, it shouldn't be too complicated so it shouldn't hurt to duplicate it.