Solved

Manipulating Arrays

Posted on 1998-11-23
4
197 Views
Last Modified: 2010-05-03
Please could someone offer the shortest code to do the following:

Delete an element in the middle of a dynamic array meaning all the elements above that one in the set are pushed down one.

Test whether a variable equals that of an element in an array.


Thanks
0
Comment
Question by:Jesus112398
  • 2
4 Comments
 

Expert Comment

by:atelford
ID: 1446408
It would be mouch easier if you used a collection , then you would have no need to resize the array after a delete , but thats just my opinion
0
 
LVL 3

Accepted Solution

by:
CesarGon earned 70 total points
ID: 1446409
For the first problem: the fastest way to do that is to iterate the array from the element deleted up to the last minus one, assigning to each element the value of the next. After that, just ReDim the array to its original length minus one. Example:

  Dim a(1 To 10) As String
  ...

  'let's delete the 4th element.
  Dim l As Long
  For l = 4 To 9
    a(l) = a(l + 1)
  Next l
  ReDim a(1 To 9) As String

For the second problem: iterate the array checking one element at a time.

These are the fastest ways, as measured by me. Nevertheless, collections and/or dictionaries could do better in some cases.

Regards,
César.

0
 
LVL 1

Expert Comment

by:yfang
ID: 1446410
Comments to the proposed answer:
Redim will clear the contents of the array and you will lose the values of all the elements.  You have to use keyword PRESERVE to keep the contents of the elements:

ReDim preserve a(1 To 9) As String

Good luck

0
 
LVL 3

Expert Comment

by:CesarGon
ID: 1446411
Of course you have to use Preserve!. I forgot writing it in my code. Sorry about my mistake and thanks for pointing it out.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Excel VBA combine two working workbooks 8 60
VB6 - Convert HH:MM into Decimal 8 62
How to make an ADE file by code? 11 94
Zip Folders Using Chilkat Routines 1 58
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

829 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