Remove an element from an array

Level:
Level1

Working with arrays can be a little overwhelming at times. Add to this the fact that you want to remove an element from the middle of the array and many people begin to feel very confused and/or frustrated. The solution to this is to create a subroutine to do it for you. Then whenever you run into this situation you can simply rely on your subroutine and not have to go through the mental energy to get it done.

To demonstrate this do the following. Create a new VB6 application. Add a command button to it. Double click on the button. Notice where your cursor is at. You will be adding code in this location once we get the other code in place so remember where you are at (in the click event handler for your command button).

Add this code to the main source area for your form:

  1. Private Type my_type
  2.     field1 As String
  3.     field2 As Long
  4.     field3 As Integer
  5. End Type
  6.  
  7. Const MAX_ARRAY = 10
  8.  
  9. Dim strBuffer(0 To MAX_ARRAY - 1) As my_type
  10.  
  11. Public Sub DeleteRecordFromMyArray(RecPos As Integer, MaxRecs As Integer)
  12.         Dim I As Integer
  13.  
  14.         For I = RecPos To MaxRecs - 1
  15.                 strBuffer(I) = strBuffer(I + 1)
  16.         Next
  17.  
  18.         strBuffer(MaxRecs).field1 = ""
  19.         strBuffer(MaxRecs).field2 = 0
  20.         strBuffer(MaxRecs).field3 = 0
  21. End Sub

This code declares an array of records of type my_type. It also creates a subroutine that we can call to remove elements from the array.

To try this out go back to your command buttons click event handler (the one I told you to remember the position of). Add this line of code in the handler.

  1. DeleteRecordFromMyArray 3, MAX_ARRAY - 1

The easiest way to try this out is to set a break point, run it, and then step through each line of code. You will then be able to see exactly what is going on.

Note: The source for this was found at DreamVB which is no longer online.

If you enjoyed this post, subscribe for updates (it's free)

system analysis and design

Please help me to create a transaction program
Thank You!!

pgm1

1.Private Type my_type
2. field1 As String
3. field2 As Long
4. field3 As Integer
5.End Type
6.
7.Const MAX_ARRAY = 10
8.
9.Dim strBuffer(0 To MAX_ARRAY - 1) As my_type
10.
11.Public Sub DeleteRecordFromMyArray(RecPos As Integer, MaxRecs As Integer)
12. Dim I As Integer
13.
14. For I = RecPos To MaxRecs - 1
15. strBuffer(I) = strBuffer(I + 1)
16. Next
17.
18. strBuffer(MaxRecs).field1 = ""
19. strBuffer(MaxRecs).field2 = 0
20. strBuffer(MaxRecs).field3 = 0
21.End Sub

pgm

1.Private Type my_type
2. field1 As String
3. field2 As Long
4. field3 As Integer
5.End Type
6.
7.Const MAX_ARRAY = 10
8.
9.Dim strBuffer(0 To MAX_ARRAY - 1) As my_type
10.
11.Public Sub DeleteRecordFromMyArray(RecPos As Integer, MaxRecs As Integer)
12. Dim I As Integer
13.
14. For I = RecPos To MaxRecs - 1
15. strBuffer(I) = strBuffer(I + 1)
16. Next
17.
18. strBuffer(MaxRecs).field1 = ""
19. strBuffer(MaxRecs).field2 = 0
20. strBuffer(MaxRecs).field3 = 0
21.End Sub