Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Passing an object array to a function changes it's type?


Reply
 
Thread Tools Display Modes
  #1  
Old 06-05-2008, 02:04 AM
Joakim Nygren Joakim Nygren is offline
Newcomer
 
Join Date: Jun 2008
Posts: 1
Default Passing an object array to a function changes it's type?

Hi, thanks for reading.

I have a calculation program that has over a thousand in varibles, sorted in 15 or so classes that all inherit from a data class. These are used in arrays in the main program since the program should contain several runs.

When writing the parameters to disc I pass the object array (of any of the 15 types) and then write a text file with the fields.

When reading parameters I need to resize the arrays depending on what I find in the text files. But at the end of the function I got errors saying that system.array[] could not be converted to <my custom class>[].

Then i noticed that the array passed into the function has changed to a system.array[] directly on entering the function. Why is that, and how can I avoid it?

Another qustion, how can I make a new object in the array without beforehand knowing the type? I tried using gettype but that didn't work, my workaround now is having a select case that picks picks the type and creates it, but that is not a very "elegant" solution.

Declaration of function
Private Function ReadDataClassAllFieldsToDisk(ByVal CheckObject() As Object) As Boolean

Calling of function
ReadDataClassAllFieldsToDisk(Hissdata_Arr)

Field in the mainform
Public Hissdata_Arr(0) As Hissdata

OnLoad of the mainform
Dim Hissdata_ As New Hissdata
Hissdata_Arr(0) = Hissdata_
Reply With Quote
  #2  
Old 06-05-2008, 08:20 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is online now
Ultimate Contributor

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
Default

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.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.

Last edited by AtmaWeapon; 06-05-2008 at 09:53 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:

Powered by liquidweb