Results 1 to 3 of 3

Thread: [RESOLVED] iterate thru variables by name

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    488

    Resolved [RESOLVED] iterate thru variables by name

    Hello
    I have three arrays that I manipulate in the same way.
    I do this by calling a sub 3 times passing the name of the array one at a time from a second sub

    Code:
      Private Sub rowscol(ByVal temparray(,) As Button)
            do stuff
        End Sub
    
        Private Sub test()
            rowscol(rows)
            rowscol(cols)
            rowscol(boxes)
    
        End Sub
    This works fine!
    Is there a way to do this by using just 1 sub. Something like a for each loop that iterates thru the three arrays by name?

    Thank you
    George

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: iterate thru variables by name

    You could, I'm not sure why you would want to.
    A quick example test. I added five buttons to a form and added them to two arrays in different order.
    In the sub I then added the two arrays to an array.
    I then loop through the arrays in that array to process the buttons.
    Code:
    Public Class Form1
      Private rows() As Button
      Private cols() As Button
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        rows = {Button1, Button2, Button3, Button4, Button5}
        cols = {Button2, Button4, Button1, Button3, Button5}
      End Sub
    
      Private Sub rowscol()
        Dim tmparray()() As Button = {rows, cols}
    
        For Each temparray As Button() In tmparray
          For Each b As Button In temparray
            Debug.Print(b.Name)
          Next
        Next
      End Sub
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        rowscol()
      End Sub
    End Class
    p.s. rows, cols and boxes, looks a bit Sudoku like.
    Last edited by passel; Nov 20th, 2021 at 04:48 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: iterate thru variables by name

    Quote Originally Posted by passel View Post
    p.s. rows, cols and boxes, looks a bit Sudoku like.
    It does
    @georgesutfin… check out the sudoku link in my signature for an example

Posting Permissions

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



Click Here to Expand Forum to Full Width