Results 1 to 8 of 8

Thread: Inserting an element into an array (RESOLVED)

  1. #1

    Thread Starter
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Inserting an element into an array (RESOLVED)

    I searched the forums and found a great post dealing with a string array... but my array has a complication:

    It's a UDT array, and some of the elements of the UDT are arrays themselves. So pretty much my array looks like this:

    MyArray(1 to 2)
    MyArray(1).Name
    MyArray(1).Number
    MyArray(1).Price(1 to 5)
    MyArray(2).Name [string]
    MyArray(2).Number [single]
    MyArray(2).Price(1 to 4) [array of singles]

    Except that there are around 20 sub elements instead of 3, and there are several hundred elements instead of 2. If there's no easier way I could just loop through and manually rearrange the array as needed... but it seems like there should be some type of API solution or something, because this is a common question.
    Last edited by jemidiah; Jun 30th, 2006 at 09:31 PM. Reason: Resolved
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Inserting an element into an array

    Why don't you use a collection of a class ?

    A class for the UDT structure, and a collection to add/insert records ?

  3. #3

    Thread Starter
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Inserting an element into an array

    Because... I wrote 95% of the project years ago before I had ever heard of a class, and even now have never used one extensively. Great idea though, if I need to do this again I'll probably go with it for simplicity.

    The rewrite would be about 10 times more work intensive than even manual looping.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Inserting an element into an array

    I would suggest you to upgrade your code with vb6 and use classes, it will help you in future for debugging and even u can utilise lots of advantages over vb6...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  5. #5
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Inserting an element into an array

    It's a common question because it's a common mistake. No, the easiest way is to use a collection and a data class. Not all that hard and far easier than having to make changes to your original code and debug them too.

    What you do is place all your element names in a class (data class) and add the class to the collection. The actual data class would only contain those elements and no code. By far easier than what you are doing.

  6. #6

    Thread Starter
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Inserting an element into an array (RESOLVED)

    Just posting for anyone reading this thread in a future search.


    Randem pretty much summed the conclusion up:

    (paraphrased, see above post for original)
    Just use a data class and a collection instead of an array of UDT arrays; it's a lot simpler.

    For me, it ended up only taking 10-30 minutes or so to write the necessary 'bumping' routines, and they are fast enough with my number of elements to be unnoticeable to the user (though it bugs me to be so inefficient).

    Thanks for the suggestions, wish there was a way but apparently there isn't.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Inserting an element into an array (RESOLVED)

    Quote Originally Posted by jemidiah
    Thanks for the suggestions, wish there was a way but apparently there isn't.
    No one said it's not possible, we just said that it is not recommended... and that there are better ways to do it.

    This is an example of how it's done for your original question:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type tStruct
    4.     Str As String
    5.     DT As Date
    6. End Type
    7.  
    8. Private ArrType() As tStruct
    9.  
    10. Private Sub Form_Load()
    11.     Dim InsertType As tStruct
    12.     Dim K As Long
    13.    
    14.     ReDim ArrType(1)
    15.    
    16.     ArrType(0).DT = #4/1/2005#
    17.     ArrType(0).Str = "Hello world..."
    18.    
    19.     ArrType(1).DT = #5/1/2005#
    20.     ArrType(1).Str = "blah blah 1"
    21.    
    22.     InsertType.DT = #6/1/2005#
    23.     InsertType.Str = "blah blah 2"
    24.    
    25.     InsertStructure ArrType, InsertType, 1
    26.    
    27.     For K = 0 To UBound(ArrType)
    28.         Debug.Print K, ArrType(K).DT, ArrType(K).Str
    29.     Next K
    30. End Sub
    31.  
    32. Private Sub InsertStructure(Arr() As tStruct, Insert As tStruct, Index As Long)
    33.     Dim K As Long
    34.     ReDim Preserve Arr(UBound(Arr) + 1)
    35.    
    36.     For K = UBound(Arr) To Index + 1 Step -1
    37.         Arr(K) = Arr(K - 1)
    38.     Next K
    39.    
    40.     Arr(Index) = Insert
    41. End Sub
    It works by re-sizing the array and then "shifting" all data up after the index you want to insert.
    This works fine for small arrays, but if you have a lot of data, you will see that it is a significant decrease in processing using this method.

    The way I said first, using a collection of classes it is much faster when you have many records.

  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Inserting an element into an array (RESOLVED)

    PS, this is a related thread you might be interested in:
    http://www.vbforums.com/showthread.php?t=327833

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