Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array = Array 1

Status
Not open for further replies.

IknowMe

Programmer
Aug 6, 2004
1,214
US
Is there a way to set an array = to another array without looping through elements?

In the following code (slightly modified from a post by Skip), I'm passing an array to a function, changing it then returning the changed array.

I'd like to clean up the return some if possible. Hoping for something like sArray() = test(sArray). Thanks for any help.


Code:
Sub demo()
Dim sArray(2) As String
Dim ReturnArray As Variant
  sArray(0) = "aa"
  sArray(1) = "bb"
  sArray(2) = "cc"
  ReturnArray = test(sArray)
  
  For i = LBound(ReturnArray, 1) To UBound(ReturnArray, 1)
    sArray(i) = ReturnArray(i)
    MsgBox sArray(i)
  Next
  
End Sub

Function test(ByRef strArray As Variant) As Variant
Dim i As Integer
  For i = LBound(strArray, 1) To UBound(strArray, 1)
    strArray(i) = strArray(i) & " please work"
  Next
  test = strArray
End Function

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
As the function's parameter is passed by reference I don't understand why it is not directly updated.
Proof of concept:
Sub demo()
Dim sArray(2) As String
Dim ReturnArray As Variant
sArray(0) = "aa"
sArray(1) = "bb"
sArray(2) = "cc"
ReturnArray = test(sArray)

For i = LBound(ReturnArray, 1) To UBound(ReturnArray, 1)
[!]'[/!] sArray(i) = ReturnArray(i)
MsgBox sArray(i)
Next
End Sub

Function test(ByRef strArray As Variant) As Variant
Dim i As Integer
For i = LBound(strArray, 1) To UBound(strArray, 1)
strArray(i) = strArray(i) & " please work"
Next
test = strArray
End Function


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Something like:
Code:
Sub demo()
Dim sArray() As String
ReDim sArray(2)
Dim ReturnArray As Variant
  sArray(0) = "aa"
  sArray(1) = "bb"
  sArray(2) = "cc"
  sArray = test(sArray)
  
  For i = LBound(sArray, 1) To UBound(sArray, 1)
    MsgBox sArray(i)
  Next
  
End Sub

Function test(ByRef strArray As Variant) As String()
ReDim test(LBound(strArray) To UBound(strArray))
Dim i As Integer
  For i = LBound(strArray, 1) To UBound(strArray, 1)
    strArray(i) = strArray(i) & " please work"
  Next
  test = strArray
End Function
?

combo
 
You guys are awesome, talk about shining some light into a dark tunnel. ByVal ByRef just clicked. Thanks PHV

I ended up switching to a Sub
Code:
Sub demo()

  Dim sArray(2) As String
  sArray(0) = "aa"
  sArray(1) = "bb"
  sArray(2) = "cc"
  
  Call test(sArray)
  
  For i = LBound(sArray, 1) To UBound(sArray, 1)
    MsgBox sArray(i)
  Next
  
End Sub

Sub test(ByRef strArray As Variant)
Dim i As Integer
  For i = LBound(strArray, 1) To UBound(strArray, 1)
    strArray(i) = strArray(i) & "works like a charm"
  Next
End Sub


[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
You don't need to loop ...

Code:
Sub ArrayTest()
    Dim arr1(), arr2()
    arr1 = Array(1, 2, 3, 4, 5)
    arr2 = arr1
End Sub

..

Code:
Sub demo()
    Dim sArray(2), tArray()
    sArray(0) = "aa": sArray(1) = "bb": sArray(2) = "cc"
    tArray = sArray
'    Call test(sArray)
'    For i = LBound(sArray, 1) To UBound(sArray, 1)
'        MsgBox sArray(i)
'    Next
End Sub

You just can't define the dimension of the array until runtime (as opposed to compile/declaration time).

HTH

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top