[RESOLVED] Adding new item to array
Hi Guys!
.NET has pretty good functions to add new items to an array. Is there anyway to do it with vb6, too?
Because, you know, "Redim Preserve" is a bit bulky. It is backing up the old keys in the memory, redimensioning the array and restoring the old keys. It is a huge waste of tme. Also you need one line more to load your key to the new dimension.
I just want something like Array.Add = MyArray("3").
Any suggestions?
Thanks a lot!
Re: Adding new item to array
Use a Collection rather than an Array
Re: Adding new item to array
Dim wordsvfz() As String
wordsvfz = Split("james:love", ":")
text1.text = wordsvfz (0)
Text2.Text = wordsvfz (1)
:p:eek::p:eek::p:eek::p:eek::p:eek::p:eek:
Re: Adding new item to array
Quote:
Originally Posted by
ladoo
Dim wordsvfz() As String
wordsvfz = Split("james:love", ":")
text1.text = wordsvfz (0)
Text2.Text = wordsvfz (1)
:lol::lol::lol::lol::lol::lol::lol::lol::lol::lol::lol::lol::lol::lol::lol::lol::lol::lol:
Thanks ladoo but splitting seems more bulky and won't wok for me.
Quote:
Originally Posted by
Doogle
Use a Collection rather than an Array
What is a collection? Can you give me an example? Can it hold doubles?
Re: Adding new item to array
Yes, a collection can hold almost anything--except UDTs (Types). And the items
don't have to be all of the same type, as shown below. The up side of collections is
that they are easy to use when you need to shrink or grow the list, where it is
not as easy with arrays. The down side is that they are slower than arrays.
Short example:
Code:
Option Explicit
Private Sub Form_Load()
Dim Col As New Collection
Dim Dbl As Double
Dim Btn As CommandButton
Dim V As Variant
Dbl = Atn(1) * 4 'double to store
Set Btn = cmdButton
Col.Add Dbl, "PI" 'store dbl with a string key
Col.Add Btn, "Button" 'store the button
Debug.Print Col.Item(1) ' retrieve with index (they start at 1)
Debug.Print Col.Item(2).Caption 'the button
Debug.Print Col.Item("PI") 'retrieve with key
Debug.Print Col.Item("Button").Caption
For Each V In Col 'retrieve by iterating
If TypeOf V Is Object Then
Debug.Print V.Caption
Else
Debug.Print V
End If
Next
End Sub
Re: Adding new item to array
I think I did it but it is working slower than an array. Is it normal?
Edit: Yes, far slower to add item and to call a key...
Re: Adding new item to array