|
-
Jun 27th, 2006, 09:00 PM
#1
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.
-
Jun 27th, 2006, 09:04 PM
#2
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 ?
-
Jun 27th, 2006, 09:28 PM
#3
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.
-
Jun 28th, 2006, 12:18 AM
#4
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.
-
Jun 28th, 2006, 04:08 AM
#5
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.
-
Jun 30th, 2006, 09:31 PM
#6
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.
-
Jul 1st, 2006, 07:26 PM
#7
Re: Inserting an element into an array (RESOLVED)
 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:
Option Explicit
Private Type tStruct
Str As String
DT As Date
End Type
Private ArrType() As tStruct
Private Sub Form_Load()
Dim InsertType As tStruct
Dim K As Long
ReDim ArrType(1)
ArrType(0).DT = #4/1/2005#
ArrType(0).Str = "Hello world..."
ArrType(1).DT = #5/1/2005#
ArrType(1).Str = "blah blah 1"
InsertType.DT = #6/1/2005#
InsertType.Str = "blah blah 2"
InsertStructure ArrType, InsertType, 1
For K = 0 To UBound(ArrType)
Debug.Print K, ArrType(K).DT, ArrType(K).Str
Next K
End Sub
Private Sub InsertStructure(Arr() As tStruct, Insert As tStruct, Index As Long)
Dim K As Long
ReDim Preserve Arr(UBound(Arr) + 1)
For K = UBound(Arr) To Index + 1 Step -1
Arr(K) = Arr(K - 1)
Next K
Arr(Index) = Insert
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.
-
Jul 1st, 2006, 07:34 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|