Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Problem with arraylist assignment

Problem with arraylist assignment

Scheduled Pinned Locked Moved C#
helpquestionannouncement
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    djkno3
    wrote on last edited by
    #1

    ((Rectangle)mySquares[i]).X = ((Rectangle)mySquares[i-1]).X; I have an ArrayList of Rectangles and would like to update the X co-ordinate with the old one but this assignment generates a compiler error "The left-hand side of an assignment must be a variable, property or indexer" yet the left hand side of this IS a property. Is there a reason for this? do I have to extract/assign the x co-ordinates in a for loop?

    R 1 Reply Last reply
    0
    • D djkno3

      ((Rectangle)mySquares[i]).X = ((Rectangle)mySquares[i-1]).X; I have an ArrayList of Rectangles and would like to update the X co-ordinate with the old one but this assignment generates a compiler error "The left-hand side of an assignment must be a variable, property or indexer" yet the left hand side of this IS a property. Is there a reason for this? do I have to extract/assign the x co-ordinates in a for loop?

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      If this is not already in a for loop, then how is the current value of i determined? in any event, it will fail for i=0 since there is no element whose index = -1. Perhaps we need a to see just a wee bit more of your code in order to help?;):suss:

      D 1 Reply Last reply
      0
      • R Rob Graham

        If this is not already in a for loop, then how is the current value of i determined? in any event, it will fail for i=0 since there is no element whose index = -1. Perhaps we need a to see just a wee bit more of your code in order to help?;):suss:

        D Offline
        D Offline
        djkno3
        wrote on last edited by
        #3

        for (i = mySquares.Count - 1; i > 0; i--) { ((Rectangle)mySquares[i]).X = ((Rectangle)mySquares[i-1]).X; ((Rectangle)mySquares[i]).Y = ((Rectangle)mySquares[i-1]).Y; } I'm trying to update the x,y co-ordinates of the rectangles in the arrary in a Timer.Tic() method. it tells me that my left operand has to be a property but is Rectangle.X not a property?

        B Richard DeemingR D 3 Replies Last reply
        0
        • D djkno3

          for (i = mySquares.Count - 1; i > 0; i--) { ((Rectangle)mySquares[i]).X = ((Rectangle)mySquares[i-1]).X; ((Rectangle)mySquares[i]).Y = ((Rectangle)mySquares[i-1]).Y; } I'm trying to update the x,y co-ordinates of the rectangles in the arrary in a Timer.Tic() method. it tells me that my left operand has to be a property but is Rectangle.X not a property?

          B Offline
          B Offline
          Burt Harris
          wrote on last edited by
          #4

          Interesting, try this: for (int i = mySquares.Count - 1; i > 0; i--) { Rectangle r1 = (Rectangle)mySquares[i]; Rectangle r2 = (Rectangle)mySquares[i-1]; r1.X = r2.X; r1.Y = r2.Y; }Burt Harris

          J 1 Reply Last reply
          0
          • D djkno3

            for (i = mySquares.Count - 1; i > 0; i--) { ((Rectangle)mySquares[i]).X = ((Rectangle)mySquares[i-1]).X; ((Rectangle)mySquares[i]).Y = ((Rectangle)mySquares[i-1]).Y; } I'm trying to update the x,y co-ordinates of the rectangles in the arrary in a Timer.Tic() method. it tells me that my left operand has to be a property but is Rectangle.X not a property?

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Rectangle is a struct, so that probably won't work anyway, since you're changing the coordinates of an unboxed copy of your rectangle. Try something like:

            for (i = mySquares.Count - 1; i > 0; i--)
            {
            mySquares[i] = new Rectangle(
            ((Rectangle)mySquares[i-1]).Location,
            ((Rectangle)mySquares[i]).Size);
            }


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • B Burt Harris

              Interesting, try this: for (int i = mySquares.Count - 1; i > 0; i--) { Rectangle r1 = (Rectangle)mySquares[i]; Rectangle r2 = (Rectangle)mySquares[i-1]; r1.X = r2.X; r1.Y = r2.Y; }Burt Harris

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #6

              Burt Harris (msft) wrote: Rectangle r1 = (Rectangle)mySquares[i]; Rectangle r2 = (Rectangle)mySquares[i-1]; Unfortunately that won't work either. The problem is that Rectangle is a value type so the modifications you are doing are being done to the unboxed value which is discarded. Instead, you should create a new Rectangle object and place it back in the collection.

              for (int i = mySquares.Count - 1; i > 0; i--)
              {
              Rectangle r1 = (Rectangle)mySquares[i];
              Rectangle r2 = (Rectangle)mySquares[i-1];

              mySquares\[i\] = new Rectangle(r2.X, r2.Y, 
                  r1.Width, r1.Height);
              

              }

              [Edit]I should have posted this sooner instead of catching up on CP, Richard beat me to it :P[/Edit] James - out of order -

              1 Reply Last reply
              0
              • D djkno3

                for (i = mySquares.Count - 1; i > 0; i--) { ((Rectangle)mySquares[i]).X = ((Rectangle)mySquares[i-1]).X; ((Rectangle)mySquares[i]).Y = ((Rectangle)mySquares[i-1]).Y; } I'm trying to update the x,y co-ordinates of the rectangles in the arrary in a Timer.Tic() method. it tells me that my left operand has to be a property but is Rectangle.X not a property?

                D Offline
                D Offline
                djkno3
                wrote on last edited by
                #7

                Thanks everyone for your replies it is working now (Well at least the assignment problem I had :laugh:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups