Solved

Is there a way to shrink an array to fit?

Posted on 2009-05-14
9
347 Views
Last Modified: 2013-11-26
I'm curious if there's a way to shrink an array to fit.

For example I have an array that I will be adding text into and then removing it after I process it.  So when it's processed then I will remove that item from the array (starting from the bottom).  Therefore I may have an array that starts at 5 and ends at 20.

I'm curious to know if there's a way to shrink the array with all the data in it down to an array that starts at 0 and ends at 14.

I know it can be done by copying the array over line by line with a loop, but I wasn't sure if there was a better way to do this.
0
Comment
Question by:mystami
  • 4
  • 4
9 Comments
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24390904
Redim Preserve YourArray(14)
       
0
 
LVL 1

Author Comment

by:mystami
ID: 24391103
That would provide a smaller array but would not make the information move down from 5 to now start at 0 with all the other information as well and the last one now being 14 instead of 20.
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24391151
Yea, if your pulling items out of the top or the middle the only way to shrink the array would be to reload it. Have you considered using a collection instead of an array?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:mystami
ID: 24391714
I haven't, actually, I forgot about collections!

Have you had any experience with the processing speed of Collections vs. Arrays?
0
 
LVL 1

Author Comment

by:mystami
ID: 24391759
Is there any sort of multidimension collection because I need to be able to store 2 values, one value being the winsock user and the other is the message to send.
0
 
LVL 1

Author Comment

by:mystami
ID: 24391779
Of course 2 collections can always be used but I need something that's going to run as fast as possible.
0
 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 125 total points
ID: 24397618
I would create a structure to hold your data and load the structure to a collection. See snippet.
Not sure how much data you're working with so as far as performance I can't really comment. You could also keep the array approach but make it an object array and load the structure to the array, this would eliminate the need for multiple arrays. To manage the size you could keep track of the number of items you have removed and when you hit some threshold reload the array, maybe 20% deleted records?
 

    Dim cCollection As New Collection
 
    'Structure needs to be in a Module
    Public Structure CollectionData
        Dim Field1 As String
        Dim Field2 As String
 
        Public Sub New(ByVal _Field1 As String, ByVal _Field2 As String)
            Field1 = _Field1
            Field2 = _Field2
        End Sub
 
        Public Overrides Function ToString() As String
            Return Me.Field1
        End Function
    End Structure
 
 
    Public Sub WorkWithCollection()
        Dim cData As New CollectionData
        Dim strKey As String = "Some unique key to access the item later"
 
        'Load the structure
        cData.Field1 = "Some info"
        cData.Field2 = "Some additional info"
 
        'Add to collection
        cCollection.Add(cData, strKey)
 
        'Get the data back
        cData = cCollection.Item(strKey)
 
        'Remove the item
        cCollection.Remove(strKey)
    End Sub

Open in new window

0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24540066
Did you get your question answered?
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PowerShell:  Column widths won't expand 3 50
Data organization issue 7 40
asp.net figure out error 3 26
ASP.NET Built-In Report Creator / Viewer 5 26
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

696 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