Results 1 to 4 of 4

Thread: Pushing to an Array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    28

    Resolved Pushing to an Array

    Sorry if this is a really obvious question, ive searched the forums etc but cant find a solution.

    I'm creating an array like this:

    Code:
    Dim ConnectionsArray() As String
    later on i just want to PUSH an item to the end of the array. I dont really want to reference the array number at all.

    For example:

    Code:
    ConnectionsArray.push(myvar)
    Is this possible?
    Last edited by albo; Apr 21st, 2009 at 07:50 AM.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Pushing to an Array

    Code:
    Private Sub Push(ByRef arr() as string, ByVal value as String)
        Redim Preserve arr(ubound(arr)+1)
        arr(ubound(arr)) = value
    End Sub
    And you use it like this:
    Code:
    Push ConnectionsArray, MyVar
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Pushing to an Array

    ReDim Preserving is obscenely slow for large arrays since it has to copy everything you already have. If this is an issue you can, for example, add 10 blank entries, extending the array only when necessary.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Pushing to an Array

    A collection might be more appropriate.

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