Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Creat/Resize an array without knowing the array type

Posted on 2005-03-06
8
Medium Priority
?
186 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
7 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

580 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