Results 1 to 7 of 7

Thread: [RESOLVED] Adding new item to array

  1. #1

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Resolved [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!
    Last edited by Flashbond; Feb 22nd, 2013 at 08:08 AM.

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Adding new item to array

    Use a Collection rather than an Array

  3. #3
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: Adding new item to array

    Dim wordsvfz() As String
    wordsvfz = Split("james:love", ":")
    text1.text = wordsvfz (0)
    Text2.Text = wordsvfz (1)

  4. #4

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: Adding new item to array

    Quote Originally Posted by ladoo View Post
    Dim wordsvfz() As String
    wordsvfz = Split("james:love", ":")
    text1.text = wordsvfz (0)
    Text2.Text = wordsvfz (1)
    Thanks ladoo but splitting seems more bulky and won't wok for me.


    Quote Originally Posted by Doogle View Post
    Use a Collection rather than an Array
    What is a collection? Can you give me an example? Can it hold doubles?

  5. #5
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    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

  6. #6

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    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...
    Last edited by Flashbond; Feb 22nd, 2013 at 09:01 AM.

  7. #7
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Adding new item to array

    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width