Saturday, July 20, 2013

Code Example: Different Ways to Work With Arrays

This post is inspired by this StackOverflow question asking about passing a 2D array around... Let's say you have two Subs or Functions (or one of each) between which you want to pass an array (any array) of values. Here is an example of Subs trying to do something like this, but unfortunately failing:
Sub StartSub()
Dim myValues() As Integer
' Fill the array
StopSub myValues
End Sub

Sub StopSub(otherValues As Integer)
MsgBox otherValues(2)
End Sub
The right solution to this problem is not always obvious, but there are a number of options at your disposal. Let's have a look...
comments powered by Disqus