SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict foolios's Avatar
    Join Date
    Dec 2006
    Location
    The least technologically advanced state in the US
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checking last index of the array giving me a problem. VS2005 C#

    Code:
            private void CreateMaze(int h, int w)
            {
                int r = h * w;
                Room[] rooms = new Room[r];
                Random rm = new Random();
                int newrm = rm.Next(0, r);
                int nrmcntr = newrm;
                do
                {
                    rooms[newrm].rWall = "|";
                    if (newrm == rooms.Length() -1)
                    {
                        newrm = 0;
                    }
                    newrm += 1;
                } 
                while (newrm != nrmcntr);
    At the line:
    if (newrm == rooms.Length() -1)
    I am trying to compare an integer to the last index of an array of room objects.

    I get the error:
    Method name expected
    at that line but have no clue.

    Thank you in advance for any explanations.

  2. #2
    SitePoint Addict foolios's Avatar
    Join Date
    Dec 2006
    Location
    The least technologically advanced state in the US
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, the error was that I had () following length but I do have another question taking this array a little further:

    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.

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,576
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    It should resemble foreach (char ch in str):

    Code:
    foreach (Room r in rooms)
    {
       r.rWall="|";
       r.floorDwn="_";
    }

  4. #4
    SitePoint Addict foolios's Avatar
    Join Date
    Dec 2006
    Location
    The least technologically advanced state in the US
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The array was an array of structures and as it turns out r had the value.
    And being that foreach is read-only I can't do this?

  5. #5
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,576
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You should be able to modify properties of the items in the collection, but you cannot modify the collection itself. There are a couple of ways one can code around this depending on the situation. Not really certain what you are trying to do here, so I cannot really elaborate further without more information.


Bookmarks

Posting Permissions

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