Solved

Manipulating Arrays

Posted on 1998-11-23
4
203 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
[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
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

733 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