Solved

Transferring Array Contents

Posted on 2013-11-23
11
216 Views
Last Modified: 2013-11-24
Is there a way to transfer the contents of one array to another and still keep the original array?  Here is what I want to do:

        For i = 0 To UBound(RaceBibs) - 1
            AvailBibArr(k) = RaceBibs(i)
            k = k + 1
            ReDim Preserve AvailBibArr(k)
        Next i

Open in new window


Thanks!
0
Comment
Question by:Bob Schneider
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 39671754
Yes. Your code looks OK, but and this is a little simpler and faster:

Sub txarray()
    Dim i As Integer
    Dim AvailBibArr() As Integer ' ? Same type as RaceBibs
   
    ReDim AvailBibArr(UBound(RaceBibs))
    For i = 0 To UBound(RaceBibs)
        AvailBibArr(i) = RaceBibs(i)
    Next i
End Sub
0
 
LVL 8

Assisted Solution

by:Mohit Vijay
Mohit Vijay earned 126 total points
ID: 39671769
ReDim Preserve not require for second array, as you know the size of the array, it will be same as first array, isnt it?

so declare the second array with size initially. apart from that your code looks ok. If you use C# or Visual studio, there are many other generic types and functions available to do this. but it seems you are using VB 6 or older. So your code is fine, except above size initialization.
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 250 total points
ID: 39671800
VjSoft,

Thank you for the explanation that I omitted.

Actually, there is an even simper solution, which uses the Variant type, which I try to avoid. In that method, you declare the new array as a variant.

Sub txarray()
    Dim AvailBibArr
    Dim RaceBibs(2) As String

    RaceBibs(1) = "X"
    RaceBibs(0) = "Z"
    RaceBibs(2) = "A"
    AvailBibArr = RaceBibs
    
End Sub

Open in new window

0
 
LVL 8

Assisted Solution

by:Mohit Vijay
Mohit Vijay earned 126 total points
ID: 39671808
GrahamSkan,

Agree! But this is having performance impact, because at runtime you will decide the size, you dont know the size of array at compile time. Apart from that this is also fine for small type of arrays.
0
 

Author Comment

by:Bob Schneider
ID: 39671811
Semi-related question:  If you have to include different types of data in a multi-dimensional array, do you have to user Variant data type?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 250 total points
ID: 39671843
I am always willing to learn, but I believe that all elements in an array, including multi-dimensional arrays must have the same type.
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 124 total points
ID: 39671912
Graham,

All dimensions of an array must be of the same type, but if you declare the array as type Variant (or omit a specific type in the declaration), then each element will be a variant, and a Variant element can in fact hold any data type.

:)

Patrick
0
 

Author Comment

by:Bob Schneider
ID: 39672637
How do you create an array like, say TestArr(3, 0) that is populated on the fly and data type each element?
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 124 total points
ID: 39672830
As I wrote above, if you need to accommodate a mix of data types in your array, then declare it as a Variant.  In doing so, you make each element in the array a Variant, and a Variant can store any other data type.
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 250 total points
ID: 39672835
Patrick,
Thanks for pointing that out. I had forgotten.

BobbaBuoy,
I'm not sure that I understand that query, but here is some code to demonstrate what Patrick has just described.
Dim TestArray(3, 0) As Variant
Dim i As Integer

TestArray(0, 0) = 2
TestArray(1, 0) = 1.234
TestArray(2, 0) = TestArray(0, 0) * TestArray(1, 0)
TestArray(3, 0) = "abc"
For i = 0 To 3
    Debug.Print TestArray(i, 0)
Next i

Open in new window

0
 

Author Comment

by:Bob Schneider
ID: 39672840
Thanks.  That is good clarification.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access - Capture pressed key onclick 4 28
Import csv files to MS SQL 5 74
Hide vba in gp 7 83
Problems using Provider=OraOLEDB.Oracle via VBScript/Classic ASP 5 47
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now