Hi,

I have an array that contains 5 elements:

i.e.
a(0) = "A"
a(1) = "B"
a(2) = "C"
a(3) = "D"
a(4) = "E"

What I want to be able to do is remove the first element then add information to the end.

e.g.
a(0) = "B"
a(1) = "C"
a(2) = "D"
a(3) = "E"
a(4) = "F" ; <- New


a(0) = "C"
a(1) = "D"
a(2) = "E"
a(3) = "F"
a(4) = "G" ; <- New

etc.....

Is there an easy way to remove the first element in an array?

Thanks for any help.
pG

why do you want to remove the first element and then add one to the end.

maybe it would be better to keep the 5 elements and move the data inside them?

so
temp = A(0)
A(0) = A(1)
A(1) = A(2)
A(2) = A(3)
A(3) = A(4)
A(4) = temp

why do you want to remove the first element and then add one to the end.

maybe it would be better to keep the 5 elements and move the data inside them?

so
temp = A(0)
A(0) = A(1)
A(1) = A(2)
A(2) = A(3)
A(3) = A(4)
A(4) = temp

The OP wants to add new elements too. Your code doesn't support that.

@purplegerbil

There are many inbuilt functions for string manipulations in VB6. Go to Object Browser and check out the functions in the String class.

Thanks for your reply.

This is what I have gone with:

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Form_Load()
    Dim strTest() As String
    ' some initial data
    ReDim strTest(4)
    strTest(0) = "A"
    strTest(1) = "B"
    strTest(2) = "C"
    strTest(3) = "D"
    strTest(4) = "E"
    
    ' remove the first item
    strTest(0) = vbNullString
    ' copy remaining items one step to the beginning
    CopyMemory ByVal VarPtr(strTest(0)), ByVal VarPtr(strTest(1)), UBound(strTest) * 4&
    ' mark last item to be vbNullString to avoid having two string variables pointing to the same string data
    ' (it would be crashingly dangerous to leave both to point to the same data)
    CopyMemory ByVal VarPtr(strTest(UBound(strTest))), 0&, 4&
    
    ' now add the new item
    strTest(4) = "F"
    ' show what we have
    Debug.Print Join(strTest, " + ")
End Sub

pG

I thought that you wanted the value of the last element to be assigned dynamically. But, this is also fine. Please do mark the thread as solved if it is so.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.