Results 1 to 26 of 26

Thread: For each next statement

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    karachi
    Posts
    90
    Hellofor each next statement)
    what is this statement does and
    how to use and plz example of if
    Bye

  2. #2
    Junior Member
    Join Date
    Sep 2000
    Location
    Poland
    Posts
    26
    Hi,
    U usually use 'for' loops this way:

    for iCounter=... to ...
    'code to be repeated
    Next i

    This is especially useful when U know how many times U want to repeat some code and/or when the code is dependent on iCounter.

    But in some cases U need to do some operation on every object in a collection, doesn 't matter how many objects are there

    Then 'for each ...' statement is very helpful.
    The syntax is:
    For each (var1 - usually object type) in (collection of objects of var1 type)
    'repeated code
    next (var1)

    Want an example? Here is one:

    Dim rst As Recordset
    Dim fld As Field

    Set rst = New ADODB.Recordset
    rst.OpenRecordset "Customers", dbOpenDynaset 'Customers is a name of a table in a database
    '(A)
    For Each fld In rst.Fields
    MsgBox fld.Name
    Next fld

    rst.Fields.Delete rst.Fields(3).Name
    '(B)
    For Each fld In rst.Fields
    MsgBox fld.Name
    Next fld

    in the example above, in part A, U get names of all fields in rst (columns' names);
    then U remove one column, third one
    and again U use for each statement to obtain names of all fields after deleting.

    To do the same with 'for i = ... to ...' U should know how many fields there are in the recordset before and after deleting

    The 'for each' statement is very nice, I think U'll like it

    Best wishes,
    Syl

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    For each can also be used with arrays stored in variants.
    Code:
    For each item in VariantArray
    ...
    Next item
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Originally posted by kedaman
    For each can also be used with arrays stored in variants.
    Code:
    For each item in VariantArray
    ...
    Next item
    i didn't know you could do that!

    that helps me out with some other code too!

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Have fun
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    how would i go about setting values of an array?

  7. #7
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    Well guys, I really need your help..

    I am looking for a way to do

    for each ...


    but for all the animations in a microsoft agent character.

    any idea ?
    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    set YourArray(index)=yourobject
    da_silvy, that's what you meant?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    try this:
    Code:
    For Each AnimationName In Character.AnimationNames
            List1.AddItem AnimationName
    Next
    tell me if it works!

  10. #10
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Originally posted by kedaman
    set YourArray(index)=yourobject
    da_silvy, that's what you meant?
    yes, but it does not work for me

    Code:
    for i = 1 to 20
    myArray(i) = i
    next i
    why not?

  11. #11
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    thank you da_silvy !!

    I don't get it, this property does not appear in the drop down ..

    anyway, thank you it is working.
    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  12. #12
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    that's a pleasure, but i don't know why it doesn't appear in the drop down box, must be a little secret of microsoft's

  13. #13
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    Well, probably, anyway good to have people like you around :-)

    Thanks
    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  14. #14
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    what are you using the agent for?

  15. #15
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    just make a "virtual firend" on the desktop ..

    It reads me stuff that I put in the clipboard, and from time to time do/say stuff randomly.

    I have another question that I just posted, maybe you can help me, I need to know how to use the microsoft voice recognition .., I can't make it "listen" to me (I know that you can use the commands with the characters, but I want to use the voice recognition, it seems more flexible).

    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    dasilvy, it should. What type of array are you using?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  17. #17
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    i don't know anything about it???

  18. #18
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    my last message was for kedaman!

    and now for asabi
    Originally posted by asabi
    just make a "virtual firend" on the desktop ..

    It reads me stuff that I put in the clipboard, and from time to time do/say stuff randomly.

    I have another question that I just posted, maybe you can help me, I need to know how to use the microsoft voice recognition .., I can't make it "listen" to me (I know that you can use the commands with the characters, but I want to use the voice recognition, it seems more flexible).

    you could read up about recognition on the microsoft developers website, and i will try to put some code together for you. From memory, you have to add a function to the menu, then have code for it, and assign a voice command to it.

  19. #19
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    i would also suggest downloading the complete documentation, as that is very technical (too hard in some places) and will help you will an answer.

  20. #20
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Let's make a test da_silvy:
    Code:
    Dim A as variant, B() as byte, C as variant
    
    A=Array(1,3,7)
    Redim B(2)
    B(2)=A(2)
    C=B
    Msgbox A(2) & "," & B(2) & "," & C(0)
    does this work for you?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  21. #21
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    thanks guys .. :-)
    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  22. #22
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    that example works, but how do i fix this?
    Code:
    Dim A As Variant
    Private Sub Command1_Click()
    For Each Item In A
    List1.AddItem A
    Next
    End Sub
    Private Sub Form_Load()
    For i = 0 To 20
    A(i) = i
    Next
    End Sub
    i want a(20) to = 20

    but how?

  23. #23
    Junior Member
    Join Date
    Sep 2000
    Location
    Poland
    Posts
    26
    try something like that:

    Code:
    Dim a(20) As Variant
    For i = 0 To 20
       a(i) = i
    Next i
    or

    Code:
    Dim a() As Variant
    ReDim Preserve a(1) As Variant
    For i = 0 To 20
        ReDim Preserve a(UBound(a) + 1) As Variant
        a(i) = i
    Next i


  24. #24
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    yes, thanks, that was what i was looking for

  25. #25
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That's an array of variants, not an array stored in a variant! not really what you wanted or?

    Anyway here's the array variant, in your code:
    Code:
    Dim A As Variant
    Private Sub Command1_Click()
    For Each Item In A
    List1.AddItem Item'not A
    Next
    End Sub
    Private Sub Form_Load()
    Dim B(20) as integer
    For i = 0 To 20
    B(i) = i
    Next
    A=B
    End Sub
    note, arrays of variants are really slow, don't use them, i made it a integer array for your purpose.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  26. #26
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    thanks kedaman

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