Tuesday, March 13, 2012

Remove an element from an array

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:

Dim strBuffer(0 To MAX_ARRAY - 1) As my_typePublic Sub DeleteRecordFromMyArray(RecPos As Integer, MaxRecs As Integer)                strBuffer(I) = strBuffer(I + 1)        strBuffer(MaxRecs).field1 = ""

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.

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.


View the original article here

No comments:

Post a Comment