?
Solved

Creat/Resize an array without knowing the array type

Posted on 2005-03-06
8
Medium Priority
?
184 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:jjacksn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Author Comment

by:jjacksn
ID: 13469527
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
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13469594
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.
0
 
LVL 5

Author Comment

by:jjacksn
ID: 13471160
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:Wim_Bl
ID: 13479502
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
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483864
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)

0
 
LVL 5

Author Comment

by:jjacksn
ID: 13493726
yes, exactly.  and when testArr is nothing, i'm getting a typecast error, because its not of type Object()
0
 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13493843
Could you show me what exactly you're doing different to what im doing... ok, it's a stripped down class ... but this is working for me

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code .....

    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

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myBigObj As New clsObjContainer()

        AddObjectToArray("TEST", myBigObj.myArrayOfMyObjs)

        MsgBox(myBigObj.myArrayOfMyObjs(0))
    End Sub
End Class

Class clsObjContainer
    Public myArrayOfMyObjs As Object()
End Class

=========

Sorry if i missed something completely... been up all night
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question