Beginner VB

Moderators: None (Apply to moderate this forum)
Number of threads: 1233
Number of posts: 2978

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help help please please !!!!! Posted by Tshabalala on 9 Sept 2008 at 12:35 AM
Is it possible to use the For Each loop for multi dimensional Arrays
Report
Re: Help help please please !!!!! Posted by dokken2 on 9 Sept 2008 at 8:06 AM
: Is it possible to use the For Each loop for multi dimensional Arrays
:

yes, see example-


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
Report
Re: Help help please please !!!!! Posted by Barkeeper on 10 Sept 2008 at 1:55 AM
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-
:
:
:
: 
: 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
: 
:

------------------------------------------
Only stupidity of mankind and the universe
are infinite, but i'm not sure concerning
the universe. A. Einstein
Report
Re: Help help please please !!!!! Posted by BitByBit_Thor on 12 Sept 2008 at 2:49 PM
: 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:
Dim someArray() As SomeTypeHere
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



Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Help help please please !!!!! Posted by Barkeeper on 15 Sept 2008 at 12:52 AM
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:
:
: 
: Dim someArray() As SomeTypeHere
: 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
: 
:
:
:
: 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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.