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
  1. Home
  2. General Programming
  3. C#
  4. gdi+ shape is not a reference object in Arraylist?

gdi+ shape is not a reference object in Arraylist?

Scheduled Pinned Locked Moved C#
questionwinformsgraphicsdebuggingannouncement
7 Posts 3 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.
  • S Offline
    S Offline
    smallkubi
    wrote on last edited by
    #1

    Rectangle a=new Rectangle(); Arraylist b=new Arraylist; b.add(a); a.X=1; When i debug these codes, i found b[0] is not a 's reference, they are not same, b[0].x is 0, not 1. I am fuzzy to gdi+, i need put all shapes in a Arraylist, when they are update(move or change shapes),i can re-draw all of it in a canvas. So how can i do it? :)

    L G 2 Replies Last reply
    0
    • S smallkubi

      Rectangle a=new Rectangle(); Arraylist b=new Arraylist; b.add(a); a.X=1; When i debug these codes, i found b[0] is not a 's reference, they are not same, b[0].x is 0, not 1. I am fuzzy to gdi+, i need put all shapes in a Arraylist, when they are update(move or change shapes),i can re-draw all of it in a canvas. So how can i do it? :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The problem is caused by the Rectangle being a structure (i.e. a Value type). So when you add it to the ArrayList it stores the values of the variable rather than a reference.

      S 1 Reply Last reply
      0
      • L Lost User

        The problem is caused by the Rectangle being a structure (i.e. a Value type). So when you add it to the ArrayList it stores the values of the variable rather than a reference.

        S Offline
        S Offline
        smallkubi
        wrote on last edited by
        #3

        Then how can i do if i want add all different shapes in a list? i need to update properties of them.

        L 1 Reply Last reply
        0
        • S smallkubi

          Rectangle a=new Rectangle(); Arraylist b=new Arraylist; b.add(a); a.X=1; When i debug these codes, i found b[0] is not a 's reference, they are not same, b[0].x is 0, not 1. I am fuzzy to gdi+, i need put all shapes in a Arraylist, when they are update(move or change shapes),i can re-draw all of it in a canvas. So how can i do it? :)

          G Offline
          G Offline
          George Jonsson
          wrote on last edited by
          #4

          First of all ArrayList is not recommended as the performance is not the best. See the Remarks section in ArrayList Class[^] That said, as you cannot change the values as the Rectangle is a struct and not a class, it might be better if you implement an interface and/or an abstract base class, and then a class for each shape you want to have, Rectangle, Circle, Triangle etc. (Or whatever it is you want) The specific shape classes all inherit the base class. The benefit of this approach is that you can share common properties, such as Position and Size but also implement shape specific a Paint method for each class.

          public abstract class Base
          {
          public Base()
          {
          location = new Point();
          }

          public abstract void Paint(Graphics g);
          
          public string ShapeName { get; protected set; }
          
          private Point location;
          public Point Location
          {
              get
              {
                  return location;
              }
              set
              {
                  location = value;
              }
          }
          
          private Size size;
          public Size Size
          {
              get
              {
                  return size;
              }
              set
              {
                  size = value;
              }
          }
          
          
          public int X
          {
              get
              {
                  return location.X;
              }
              set
              {
                  location.X = value;
              }
          }
          

          }

          public class Square : Base
          {
          public Square(Point location, Size size)
          : base()
          {
          ShapeName = "Square";
          Location = location;
          Size = size;
          }

          public override void Paint(Graphics g)
          {
              // Implement the way to draw a square
          }
          

          }

          Then

          List shapes = new List();
          shapes.Add(new Square(new Point(), new Size(30, 30)));
          shapes[0].X = 10;

          S 1 Reply Last reply
          0
          • S smallkubi

            Then how can i do if i want add all different shapes in a list? i need to update properties of them.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            See George's suggestion below.

            1 Reply Last reply
            0
            • G George Jonsson

              First of all ArrayList is not recommended as the performance is not the best. See the Remarks section in ArrayList Class[^] That said, as you cannot change the values as the Rectangle is a struct and not a class, it might be better if you implement an interface and/or an abstract base class, and then a class for each shape you want to have, Rectangle, Circle, Triangle etc. (Or whatever it is you want) The specific shape classes all inherit the base class. The benefit of this approach is that you can share common properties, such as Position and Size but also implement shape specific a Paint method for each class.

              public abstract class Base
              {
              public Base()
              {
              location = new Point();
              }

              public abstract void Paint(Graphics g);
              
              public string ShapeName { get; protected set; }
              
              private Point location;
              public Point Location
              {
                  get
                  {
                      return location;
                  }
                  set
                  {
                      location = value;
                  }
              }
              
              private Size size;
              public Size Size
              {
                  get
                  {
                      return size;
                  }
                  set
                  {
                      size = value;
                  }
              }
              
              
              public int X
              {
                  get
                  {
                      return location.X;
                  }
                  set
                  {
                      location.X = value;
                  }
              }
              

              }

              public class Square : Base
              {
              public Square(Point location, Size size)
              : base()
              {
              ShapeName = "Square";
              Location = location;
              Size = size;
              }

              public override void Paint(Graphics g)
              {
                  // Implement the way to draw a square
              }
              

              }

              Then

              List shapes = new List();
              shapes.Add(new Square(new Point(), new Size(30, 30)));
              shapes[0].X = 10;

              S Offline
              S Offline
              smallkubi
              wrote on last edited by
              #6

              Thank you very much. I can't understand very well, you mean that the best method is i need new a rectangle class inherit from BaseShape ,use location and Size property, and no need to use rectangle struct ,is it right?

              G 1 Reply Last reply
              0
              • S smallkubi

                Thank you very much. I can't understand very well, you mean that the best method is i need new a rectangle class inherit from BaseShape ,use location and Size property, and no need to use rectangle struct ,is it right?

                G Offline
                G Offline
                George Jonsson
                wrote on last edited by
                #7

                You can also use a Rectangle struct internally inside the base class. I just made a quick example to show the concept.

                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