Differences between Constructors and Properties

Nov 22 2009 7:09 PM
I would like to know the difference between Constructors and Properties. All I know is that a constructor is used to create an instance of the object, but my question is how?
 
I'm trying to understand the whole concept of object-oriented programming and I just can't seem to get the grasp on properties. I know how to use methods, but I would like to know how they differ from properties. I've noticed when I use properties, I need to have a "get" and or "set." But These 2 keywords I don't understand how they work. I'll post a copy of my code. It works and does what it supposed to do, but I don't understand  how it is getting it's information. Here's a copy of the code:
 
This is  the base class called Sprite.
 
  1. namespace WindowsGame1  
  2. {  
  3.     abstract class Sprite  
  4.     {  
  5.         // All of these values are passed to each of the derived classes.  
  6.         Texture2D textureImage;  
  7.         public Vector2 position;  
  8.         protected Point frameSize;  
  9.         protected Vector2 speed;  
  10.         int collisionOffset;  
  11.         Point currentFrame;  
  12.         Point sheetSize;  
  13.         int timeSinceLastFrame = 0;  
  14.         int millisecondsPerFrame = 10;  
  15.         public string collisionCueName { getprivate set; }  
  16.         const int defaultMillisecondsPerFrame = 8;  
  17.   
  18.         // This constructor calls the next constructor while passing all of it's arguements to it.  
  19.         public Sprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed, string collisionCueName)   
  20.         : this(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, defaultMillisecondsPerFrame, collisionCueName)  
  21.         {  
  22.         }  
  23.   
  24.         // This constructer is automatically called by the first constructer. This contructor only has one extra perameter at the end.  
  25.         public Sprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed, int millisecondsPerFrame, string collisionCueName)  
  26.         {  
  27.             this.textureImage = textureImage;  
  28.             this.position = position;  
  29.             this.frameSize = frameSize;  
  30.             this.collisionOffset = collisionOffset;  
  31.             this.currentFrame = currentFrame;  
  32.             this.sheetSize = sheetSize;  
  33.             this.speed = speed;  
  34.             this.collisionCueName = collisionCueName;  
  35.             this.millisecondsPerFrame = millisecondsPerFrame;  
  36.         }  
  37.   
  38.         public virtual void Update(GameTime gameTime, Rectangle clientBounds)  
  39.         {  
  40.             timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;  
  41.             if (timeSinceLastFrame > millisecondsPerFrame)  
  42.             {  
  43.                 timeSinceLastFrame = 0;  
  44.                 ++currentFrame.X;  
  45.   
  46.                 if (currentFrame.X >= sheetSize.X)  
  47.                 {  
  48.                     currentFrame.X = 0;  
  49.                     ++currentFrame.Y;  
  50.                     if (currentFrame.Y >= sheetSize.Y)  
  51.                     {  
  52.                         currentFrame.Y = 0;  
  53.                     }  
  54.                 }  
  55.             }  
  56.         }  
  57.   
  58.         public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)  
  59.         {  
  60.             spriteBatch.Draw(textureImage, position, new Rectangle(currentFrame.X * frameSize.X, currentFrame.Y * frameSize.Y, frameSize.X, frameSize.Y), Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);  
  61.         }  
  62.   
  63.         public abstract Vector2 Direction  
  64.         {  
  65.             get;  
  66.         }  
  67.   
  68.         public Rectangle CollisionRect  
  69.         {  
  70.             get  
  71.             {  
  72.                 // "(int)position.X + collisionOffset - 10": shaves 10 pixels off the sides of collisionBox of the object being created.  
  73.                 // When creating the object the collisionOffset variable is used to see where to start drawing the rectangle.  
  74.                 return new Rectangle((int)position.X + collisionOffset - 10, (int)position.Y + collisionOffset, frameSize.X - (collisionOffset * 2), frameSize.Y - (collisionOffset * 2));  
  75.             }  
  76.         }  
  77.   
  78.         public bool IsOutOfBounds(Rectangle clientRect)  
  79.         {  
  80.             if (position.X < -frameSize.X || position.X > clientRect.Width || position.Y < -frameSize.Y || position.Y > clientRect.Height)  
  81.             {  
  82.                 return true;  
  83.             }  
  84.             else  
  85.             {  
  86.                 return false;  
  87.             }  
  88.         }  
  89.   
  90.         public Vector2 GetPosition  
  91.         {  
  92.             get { return position; }  
  93.         }  
  94.     }  
  95. }  
And here is the derived class called userControlledSprite:
 
  1. namespace WindowsGame1  
  2. {  
  3.     class UserControlledSprite : Sprite  
  4.     {  
  5.         MouseState prevMouseState;  
  6.         protected Vector2 velocity = new Vector2(0, -5);  
  7.         protected Vector2 prevVelocity = new Vector2 (0, -5);  
  8.         protected Vector2 acceleration = new Vector2(0, -0.10f);  
  9.         protected Vector2 gravity = new Vector2(0, 0.30f);  
  10.         protected int timeSinceLastJump = 0;  
  11.         bool onFloor = false;  
  12.   
  13.         // These Constructors access all of the variables in the base class. sprite.cs  
  14.         public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed)  
  15.         : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed,  null)  
  16.         {  
  17.         }  
  18.   
  19.         // Same here.  
  20.         public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed, int millisecondsPerFrame)  
  21.         : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, millisecondsPerFrame, null)  
  22.         {  
  23.         }  
  24.   
  25.         // All of these properties must do exactly what the base class defines, and any anything extra can go here. Except for the Update and Draw methods.  
  26.         public override Vector2 Direction  
  27.         {  
  28.             get  
  29.             {  
  30.                 // Creates a variable to store a direction to be passed back to the sprite base class.  
  31.                 Vector2 inputDirection = Vector2.Zero;  
  32.                 GameTime gameTime = new GameTime();  
  33.   
  34.                 // This adds gravity at all times to the player to make sure he's not standing in thin air.  
  35.                 if (onFloor == false)  
  36.                 {  
  37.                     inputDirection.Y += gravity.Y * 8;  
  38.                 }  
  39.                 else  
  40.                 {  
  41.                     inputDirection.Y = 0;  
  42.                 }  
  43.   
  44.                 // Simple keyboard checks here, for left, right, up and down arrow keys.  
  45.                 if (Keyboard.GetState().IsKeyDown(Keys.Left))  
  46.                 {  
  47.                     inputDirection.X -= 1;  
  48.                 }  
  49.                 if (Keyboard.GetState().IsKeyDown(Keys.Right))  
  50.                 {  
  51.                     inputDirection.X += 1;  
  52.                 }  
  53.                 if (Keyboard.GetState().IsKeyDown(Keys.Up))  
  54.                 {  
  55.                     inputDirection.Y -= 1;  
  56.                 }  
  57.                 if (Keyboard.GetState().IsKeyDown(Keys.Down))  
  58.                 {  
  59.                     inputDirection.Y += 1;  
  60.                 }  
  61.   
  62.                 // Creates a gamepad state for player one on an Xbox 360 controller.  
  63.                 GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);  
  64.   
  65.                 // This first if statement checks if the thumbsticks have moved.  
  66.                 // Idle = 0, Left = -1, Right = 1.  
  67.                 // The above values are the maximum values for the thumbsticks.  
  68.                 if (gamePadState.ThumbSticks.Left.X != 0)  
  69.                 {  
  70.                     inputDirection.X += gamePadState.ThumbSticks.Left.X;  
  71.   
  72.                     // This takes the value of the thumbsticks and adds it to the X-velocity for side-to-side movement.  
  73.                     velocity.X += gamePadState.ThumbSticks.Left.X;  
  74.   
  75.                     if (velocity.X > 0.5f || velocity.X < -0.5f)  
  76.                     {  
  77.                         if (gamePadState.ThumbSticks.Left.X > 0)  
  78.                         {  
  79.                             velocity.X = 0.5f;  
  80.                         }  
  81.                         if (gamePadState.ThumbSticks.Left.X < 0)  
  82.                         {  
  83.                             velocity.X = -0.5f;  
  84.                         }  
  85.                     }  
  86.                 }  
  87.   
  88.                 // This makes sure that the player is currently on the floor before even being able to move upwards. (This will probably be taken out soon.)  
  89.                 /* if (gamePadState.ThumbSticks.Left.Y != 0 && onFloor == false) 
  90.                 { 
  91.                     inputDirection.Y -= gamePadState.ThumbSticks.Left.Y; 
  92.                 } */  
  93.   
  94.                 if (gamePadState.Buttons.A == ButtonState.Pressed && timeSinceLastJump >= 500)  
  95.                 {  
  96.                     onFloor = false;  
  97.   
  98.                     if (velocity.Y >= prevVelocity.Y)  
  99.                     {  
  100.                         velocity = prevVelocity;  
  101.                     }  
  102.   
  103.                     inputDirection.X += velocity.X;  
  104.                     inputDirection.Y += velocity.Y;  
  105.                     velocity += acceleration;  
  106.   
  107.                     timeSinceLastJump = 0;  
  108.                 }  
  109.                 else if (gamePadState.Buttons.A == ButtonState.Released)  
  110.                 {  
  111.                     inputDirection.Y += gravity.Y;  
  112.                 }  
  113.   
  114.                 if (gamePadState.Buttons.B == ButtonState.Pressed)  
  115.                 {  
  116.                     inputDirection.X += gamePadState.ThumbSticks.Left.X * 3;  
  117.                     inputDirection.Y -= gamePadState.ThumbSticks.Left.Y * 3;  
  118.                     GamePad.SetVibration(PlayerIndex.One, 1, 1);  
  119.                 }  
  120.                 else  
  121.                 {  
  122.                     GamePad.SetVibration(PlayerIndex.One, 0, 0);  
  123.                 }  
  124.   
  125.                 return inputDirection * speed;  
  126.             }  
  127.         }  
  128.   
  129.         public void collision(Rectangle player, Rectangle otherObject, GraphicsDevice graphicsDevice)  
  130.         {  
  131.             if (player.Intersects(otherObject) || player.Bottom >= graphicsDevice.PresentationParameters.BackBufferHeight)  
  132.             {  
  133.                 onFloor = true;  
  134.                 position.X = position.X;  
  135.                 position.Y = otherObject.Y - frameSize.Y;  
  136.             }  
  137.             if (player.Bottom > otherObject.Top - 1)  
  138.             {  
  139.                 onFloor = false;  
  140.             }  
  141.         }  
  142.   
  143.         public override void Update(GameTime gameTime, Rectangle clientBounds)  
  144.         {  
  145.             timeSinceLastJump += gameTime.ElapsedGameTime.Milliseconds;  
  146.             GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);  
  147.   
  148.             // Move the sprite according to the direction property.  
  149.             position += Direction;  
  150.   
  151.             // If the mouse moved, set the position of the sprite to the mouse position.  
  152.             MouseState currentMouseState = Mouse.GetState();  
  153.   
  154.             if (currentMouseState.X != prevMouseState.X || currentMouseState.Y != prevMouseState.Y)  
  155.             {  
  156.                 //position = new Vector2(currentMouseState.X, currentMouseState.Y);  
  157.             }  
  158.   
  159.             prevMouseState = currentMouseState;  
  160.   
  161.             // Keeps the sprite inside of the game window.  
  162.             if (position.X < 0)  
  163.             {  
  164.                 position.X = 0;  
  165.             }  
  166.             if (position.X > clientBounds.Width - frameSize.X)  
  167.             {  
  168.                 position.X = clientBounds.Width - frameSize.X;  
  169.             }  
  170.             if (position.Y < 0)  
  171.             {  
  172.                 position.Y = 0;  
  173.             }  
  174.             if (position.Y > clientBounds.Height - frameSize.Y)  
  175.             {  
  176.                 position.Y = clientBounds.Height - frameSize.Y;  
  177.             }  
  178.   
  179.             base.Update(gameTime, clientBounds);  
  180.         }  
  181.     }  
  182. }  
 
I would just like an explanation of how the get and set methods are working. I was following a tutorial when I wrote this code, but I have modified a little of it myself. Any help would be appreciated. 

Answers (2)