Link to home
Start Free TrialLog in
Avatar of jjacksn
jjacksn

asked on

Creat/Resize an array without knowing the array type

I want to do the following,

I have a class with many member variables that are arrays of typed objects.  I am constantly have to check if I should resize the array to add another object or create a new array.  So I was thinkin about creating the following Utility class.  But, I don't know how to create the array properly in the first if block.  

Also, will the byref call work like i want it to if I call

AddObjectToArray(myObj, myBigObj.myArrayOfMyObjs) with myBigObj.myArrayOfMyObjs as Nothing?


        Public Shared Sub AddObjectToArray(ByVal obj As Object, ByRef array As Object())
            If (array Is Nothing) Then
                'create array here
            Else
                'resize the array and add this in
                ReDim Preserve array(array.Length)
                array(array.Length - 1) = obj
            End If
        End Sub
Avatar of jjacksn
jjacksn

ASKER

actually, will this work?

     Public Shared Sub AddObjectToArray(ByVal obj As Object, ByRef array As Object())
            If (array Is Nothing) Then
                ReDim array(0)
                array(0) = obj
            Else
                'resize the array and add this in
                ReDim Preserve array(array.Length)
                array(array.Length - 1) = obj
            End If
        End Sub
Why don't you use the ArrayList class which is designed exactly for this purpose? ArrayList class works like array which length is not restricted.
Avatar of jjacksn

ASKER

Its not strongly typed which, while not neccessary, makes some of the serialization stuff we are doing much harder.  

the above code is not significant overhead anyways.  My bigger problem is that I still have the problem of passing in Nothing and wanting to get something.  

When I call the above method with Nothing as the array, it throws an error.  I basically want to pass in the address of the pointer.
Hi,

search on the "byref OUT myArray as ..."
If I got it right, the OUT keyword indicates that you're reference can point to nothing when calling the function.

greetings
Sorry, just want you to clarify something...

"When I call the above method with Nothing as the array, it throws an error.  I basically want to pass in the address of the pointer."

.....

do you mean you called your method somethinglike this?

Dim testArr() As Object

AddObjectToArray(myObj, testArr)

Avatar of jjacksn

ASKER

yes, exactly.  and when testArr is nothing, i'm getting a typecast error, because its not of type Object()
ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial