Help help please please !!!!! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help help please please !!!!!

Is it possible to use the For Each loop for multi dimensional Arrays

Comments

  • dokken2dokken2 Posts: 532Member
    : Is it possible to use the For Each loop for multi dimensional Arrays
    :

    yes, see example-


    [code]
    Option Explicit


    Dim a(1 To 2, 1 To 5) As String


    Private Sub Command1_Click()
    Dim i As Integer, j As Integer

    For i = 1 To 2
    For j = 1 To 5
    'assign value to array
    a(i, j) = CStr(i) & "," & CStr(j)
    Next j
    Next i

    For i = 1 To 2
    For j = 1 To 5
    'show array value
    MsgBox a(i, j)
    Next j
    Next i

    End Sub
    [/code]
  • BarkeeperBarkeeper Posts: 335Member
    I don't think this is what he was asking for.
    He was asking for the "For Each"-Syntax.


    : : Is it possible to use the For Each loop for multi dimensional Arrays
    : :

    Yes, it is possible, but the solution contains a death-defying hack, which you as a beginner shouldn't even consider using it.

    :
    : yes, see example-
    :
    :
    : [code]:
    : Option Explicit
    :
    :
    : Dim a(1 To 2, 1 To 5) As String
    :
    :
    : Private Sub Command1_Click()
    : Dim i As Integer, j As Integer
    :
    : For i = 1 To 2
    : For j = 1 To 5
    : 'assign value to array
    : a(i, j) = CStr(i) & "," & CStr(j)
    : Next j
    : Next i
    :
    : For i = 1 To 2
    : For j = 1 To 5
    : 'show array value
    : MsgBox a(i, j)
    : Next j
    : Next i
    :
    : End Sub
    : [/code]:

    ------------------------------------------
    Only stupidity of mankind and the universe
    are infinite, but i'm not sure concerning
    the universe. A. Einstein
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : I don't think this is what he was asking for.
    : He was asking for the "For Each"-Syntax.
    :
    :
    : : : Is it possible to use the For Each loop for multi dimensional Arrays
    : : :
    :
    : Yes, it is possible, but the solution contains a death-defying hack,
    : which you as a beginner shouldn't even consider using it.
    :

    Actually, the following (fairly simple) code (which doesn't seem hacked) seems to work:
    [code]
    Dim someArray() As [italic]SomeTypeHere[/italic]
    Dim vEnumerator As Variant 'Must be Variant for For...Each to work!

    'Fill someArray here
    '...

    For Each vEnumerator In someArray
    'Do something with the element, for instance annoy the user
    ' with a Message Box
    MsgBox vEnumerator
    Next
    [/code]


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • BarkeeperBarkeeper Posts: 335Member
    Yes, it could work in .NET, since in .NET all Datatypes are kind of an object, and the For-Each-Syntax works only for Objects.

    : Actually, the following (fairly simple) code (which doesn't seem
    : hacked) seems to work:
    : [code]:
    : Dim someArray() As [italic]SomeTypeHere[/italic]
    : Dim vEnumerator As Variant 'Must be Variant for For...Each to work!
    :
    : 'Fill someArray here
    : '...
    :
    : For Each vEnumerator In someArray
    : 'Do something with the element, for instance annoy the user
    : ' with a Message Box
    : MsgBox vEnumerator
    : Next
    : [/code]:
    :
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    ------------------------------------------
    Only stupidity of mankind and the universe
    are infinite, but i'm not sure concerning
    the universe. A. Einstein
Sign In or Register to comment.