Results 1 to 8 of 8

Thread: [RESOLVED] How do you cycle through an array of objects?

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Resolved [RESOLVED] How do you cycle through an array of objects?

    Code:
                foreach (Room q in rooms)
                {
                    rooms[q].rWall = "|";
                    rooms[q].floorDwn = "_";
                }
    When I try Room I get the error:
    Cannot implicitly convert type 'MazeGen.Room' to 'int'

    And when I try int q in rooms I get the error:
    Cannot convert type 'MazeGen.Room' to 'int'

    Thank you in advance for showing me how to cycle through the index of an array.
    I have seen it to be done like this:
    foreach (char ch in str)

    but I'm not able to resemble it in this case.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do you cycle through an array of objects?

    The whole point of a foreach loop is that the loop counter IS the item from the list. Youre' treating it like a for loop, where the loop counter is an index that you use to get the item. As this part says:
    Code:
    foreach (Room q in rooms)
    the q variable IS the Room object, not an index into the array to get a Room object. Your code should be:
    C# Code:
    1. foreach (Room q in rooms)
    2.             {
    3.                 q.rWall = "|";
    4.                 q.floorDwn = "_";
    5.             }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: How do you cycle through an array of objects?

    I tried to do as you have written but I am getting the error:

    Cannot modify members of 'q' because it is a 'foreach iteration variable'

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do you cycle through an array of objects?

    Can you show us the type definition for Room?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do you cycle through an array of objects?

    OK, I just did some testing of my own and it seems that you can set fields/properties of reference types, i.e. classes, in a foreach loop like that but you can't do so with value types, i.e. structures. Thus I assume that your Room type is a structure.

    This is explained by the fact that in your code q will be a copy of the actual value in the array, so setting its fields/properties is pointless because it will not affect the original. If you want to change fields/properties of an array of structures then use a for loop instead of a foreach loop:
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: How do you cycle through an array of objects?

    You are right. It is an array of structures.

    I didn't realize this before but your explanation was very helpful.


    Thanks so much.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] How do you cycle through an array of objects?

    You shouldn't use structures unless its absolutely essential that they have value type semantics. Reference types are superior in most situations.

  8. #8

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: [RESOLVED] How do you cycle through an array of objects?

    Thank you for that advice.

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