Results 1 to 9 of 9
  1. #1
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Using argument in an expression (VB 6)

    Maybe I'm trying to do the impossible, but here it is. I'm trying to copy an image array from one form to another with code like this:

    picAnswer(i).Picture = frmTargets.imgMisc(Index)

    This works OK. but what I would like to do is pass an array name (strImageArrayName) into this code and replace "imgTargets" with it. something like this:

    picAnswer(i).Picture = frmTargets."strArrayName"(Index)

    That way I can change the target image array.

    I haven't figured out how to do this. I tried

    picAnswer(i).Picture = "frmTargets." & strArrayName & ".(Index)" but that doesn' work.

    Any suggestions?

  2. #2
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Using argument in an expression (VB 6)

    One possibility would be to put all the arrays you might want to refer to into a collection and have the "name" of each array be that array's Index in the collection. Here's a sample:
    <pre> Dim aX(1 To 2) As Long
    Dim aY(1 To 2) As Long
    Dim colX As Collection

    Set colX = New Collection

    aX(1) = 1
    aX(2) = 2
    aY(1) = 100
    aY(2) = 200
    colX.Add aX, "OneDigitArray"
    colX.Add aY, "ThreeDigitArray"

    Debug.Print colX("ThreeDigitArray")(2)</pre>


  3. #3
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Using argument in an expression (VB 6)

    I'm afraid I'm grossly ignorant about collections, so I don't fully understand your suggestion. However, putting everything in one array and using the index to get to different groups in the array will certainly work. The only problem I see is that if I add or remove any item in a group, then the array indiexes will shift from that point to the end of the array. That will make things rather difficult. I would still prefer some way to pass an array name into my code if that is possible.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Using argument in an expression (VB 6)

    You can't pass an array name, but you can pass an array itself into a variant variable. Then you can use the variable to reference the elements of the array. Array indexes don't shift at all, so I have no idea what you mean by this
    <hr> if I add or remove any item in a group, then the array indiexes will shift from that point to the end of the array<hr>
    Charlotte

  5. #5
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Using argument in an expression (VB 6)

    Hi Charlotte,

    I'm not familiar with passing an array into a variant variable, but I'll look into it. That might solve the problem.

    The reference to shifting indexes related to another suggestion. Rather than pass an array name, I can simply put all my elements into a single array and address the elements by their index. This works OK as long as I don't add or remove any element in the array, because if I do then reference to indexes from that point on will be changed (shifed).

  6. #6
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Using argument in an expression (VB 6)

    Since you indicated you didn't fully understand my suggestion, I'll take another stab at it by providing some sample code that's more specifically adapted to your example.

    Part of your confusion may have resulted from the fact that I referred to "that array's index in the collection" when I should have referred to "that array's key in the collection." Note that the "key" of an item in a collection is an optional string that you can provide (as the 2nd argument) when you add an item to a collection. In effect, it's a way to give items "names" that you use to refer to them, as an alternative to referring to their indices. The following code adds three image arrays to colX, then uses the name (i.e., the key) of one of them to assign one of its images to your picAnswer(i).Picture:
    <pre> colX.Add frmTargets.imgDogs, "DogPictures"
    colX.Add frmTargets.imgBabes, "BabePictures"
    colX.Add frmTargets.imgSunsets, "SunsetPictures"
    strArrayName = "SunsetPictures"
    picAnswer(i).Picture = colX(strArrayName)(Index)</pre>

    Since colX("SunsetPictures") means the item in colX with "SunsetPictures" as its key, it refers to frmTargets.imgSunsets. Accordingly, the last line is equivalent to picAnswer(i).Picture = frmTargets.imgSunsets(Index), but effectively uses a string "name" to refer to the array.

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Using argument in an expression (VB 6)

    The only problem with collections is their persistence. You need to clear a collection when you're finished with it or it leaks memory like crazy.
    Charlotte

  8. #8
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Using argument in an expression (VB 6)

    In other words, he should include a "Set colX = Nothing" line at the end of the procedure, correct?

    My understanding is that this is still considered "best practice" for all object variables -- i.e., that you shouldn't trust Visual Basic to clear their memory resources just because they go out of scope (although there are dissenters on that issue).

  9. #9
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Using argument in an expression (VB 6)

    Hi Steeeve,

    Thanks for the suggestion. It works like a charm, and now I've learned something about using collections.

    Thanks to Charlotte too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •