Reference an Array using a String Variable

I want to be able to pull an array using a string that I built. Is there any way to do this
Here is my example code:

dim ary1() as string
dim ary2() as string
private sub test()
dim totalLength as integer
dim i as integer
totalLength = 0
for i = 1 to 2
totalLength = totalLength + getAryLength(i)
next i
end sub

private function getAryLength(i as integer)
dim ary as string
ary = "ary" & cstr(i)
'This is the part where I want to be able to reference the array by use of a string whose value is the same as the name of the array. This onbivously doesn't work. Any suggestions/help
getAryLength = getUpper( ary )
end function

Public Function GetUpper(varArray As Variant) As Integer
Dim Upper As Integer
On Error Resume Next
Upper = UBound(varArray)
If Err.Number Then
If Err.Number = 9 Then
Upper = 0
Else
With Err
MsgBox "Error:" & .Number & "-" & .Description
End With
Exit Function
End If
Else
Upper = UBound(varArray) + 1
End If
On Error GoTo 0
GetUpper = Upper
End Function



Answer this question

Reference an Array using a String Variable

  • Joe Lutz

    You can probably do this in .NET, but I really don't think you should. It's possible to emit code, but I've not done it, myself.

    Why can't you just parse the string and look up the array

    ubound is probably a VB6 hangover. ary1.Length will do the same. You should try to transition to VB.NET as much as you can, that old stuff is only in VB.NET because Microsoft faced a revolt when they initially took it out.



  • ThomasDL

    cgraus wrote:

    Why can't you just parse the string and look up the array



    What do you mean How do I do that


  • Gregoire de Jabrun

    I'd consider creating a dictionary of arrays, and looking them up by name.



  • jrcdude

    hi,

    i think you will need to use System.Reflection namespace to creat classes on the fly, but i don't like this namespace and don't ask me why

    best regards



  • Josh Christie - MS

    cgraus wrote:

    There is no way you can create a string that looks up the array, no way at all.


    It seems like this is my answer because that is what I wanted to do. Thank you.
    Another way that I have thought to do this is by executing the code in a string. Do you know of any way to do this

    i.e.
    dim temp as string
    temp = "length = ubound(ary1)"
    execute (temp) OR temp.execute

    (ubound is a simple function that returns the length of an array without erroring out if the array is dynamic and has no elements in it)


  • CMick

    Your example is obviously contrived, because it builds a string using an int. If you're starting with a string, you can use a regular expression to find an int in the string, and then use Convert.ToInt16 to turn that part of the string into a number.

    Something like ^\d*(\d+)^\d* will find the first number and put it in a group so you can pull it out. If you could just get the number in a string, you could skip that step.

    Do you have any option here, can you just get the number as a string Where is the number coming from



  • Jim Elliott

    Great Idea! Thanks!
    (on the other hand, do you know of any way to execute code stored as a string i've needed this functionality before)


  • Nikola Kasev

    You shouldn't use the VB6 stuff that flowed into VB.NET, you should use the new .NET syntax. What is GetUpper trying to do

    I'm not following this, do you want to get a number out of a string and use it to index an array, do you want to find the index of a string in the array, or do you want to use strings as lookup into an array A dictionary/hashtable will give you string lookup into a collection. There is no way you can create a string that looks up the array, no way at all. IT looks to me like you just need to use i as a lookup, why can't you do that



  • AreaScout

    I have been trying to use
    EbExecuteLine

    but it is hard to understand.


  • Paytheon

    hi,

    you can add array to another array

    http://www.programmersheaven.com/2/Les_VBNET_8_p2

    instead of using the array name you can use the master array index

    hope this helps



  • BlueMikey

    You are right, it was contrived in order to simplify the problem and highlight the basic functionality that I needed. If you want the long version, here it is:

    I have a function that needs to execute the same code for about 11 arrays with the same naming convention. Instead of having to type the same code 11 times I wanted to create a function to make it simple. Here is the example:

    Private Sub setWHERE()
    Dim WHEREowner As String
    Dim WHEREMSA As String
    Dim WHEREanchor As String
    Dim WHEREcity As String
    Dim WHEREsymbol As String
    Dim WHEREMSAParent As String
    Dim WHEREanchorParent As String
    Dim WHEREstate As String
    Dim WHEREmanager As String
    Dim WHEREtype As String
    Dim length As Integer
    Dim i As Integer
    Dim WHERE As String
    WHEREowner = ""
    WHEREMSA = ""
    WHEREanchor = ""
    WHEREcity = ""
    WHEREsymbol = ""
    WHEREMSAParent = ""
    WHEREanchorParent = ""
    WHEREstate = ""
    WHEREmanager = ""
    WHEREtype = ""
    length = GetUpper(aryOwner)
    For i = 1 To length - 1
    If i = 1 Then
    WHEREowner = WHEREowner & "owner = '" & aryOwner(i) & "'"
    Else
    WHEREowner = WHEREowner & " OR owner = '" & aryOwner(i) & "'"
    End If
    Next i
    WHEREMSA = ""
    length = GetUpper(aryMSA)
    For i = 1 To length - 1
    If i = 1 Then
    WHEREMSA = WHEREMSA & "MSA = '" & aryMSA(i) & "'"
    Else
    WHEREMSA = WHEREMSA & " OR MSA = '" & aryMSA(i) & "'"
    End If
    Next i

    '... etc. ...
    'This code would have to be repeated for every array.
    'Instead I would like to call the function getWHERE() that I wrote below.
    'Like this:
    'WHEREowner = getWHERE("Owner")
    'WHEREMSA = getWHERE("MSA")
    '... etc. ...

    WHERE = WHEREowner & WHEREMSA '& ... etc. ...
    Me.txtWhere.Value = WHERE
    End Sub

    Private Function getWHERE(cat As String) As String
    Dim WHERE As String
    Dim ary As String
    Dim length As Integer
    Dim i As Integer
    WHERE = ""
    ary = "ary" & cat
    length = GetUpper(ary) 'OR ary.length if you prefer
    For i = 1 To length - 1
    If i = 1 Then
    WHERE = WHERE & cat & " = '" & ary(i) & "'"
    Else
    WHERE = WHERE & " OR " & cat & " = '" & ary(i) & "'"
    End If
    Next i
    getWHERE = WHERE
    End Function


  • Bill Essary MSFT

    http://www.google.com.au/search hl=en&q=dynamic+VB.NET+code+execution&meta=

    I would still maintain that for what you're doing, this is both overkill and a hack.



  • Reference an Array using a String Variable