Solved

Is there a way to shrink an array to fit?

Posted on 2009-05-14
9
340 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now