Properties menu

User Tag List

Thread: Properties

Results 1 to 5 of 5
  1. #1
    Cephalopod's Avatar Member
    Reputation
    5
    Join Date
    Sep 2008
    Posts
    85
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Properties

    Just trying to figure out when one would use a property over a field.

    I can't see why I would ever need to use a property. My book I've got here explains them but so far I see no reason why they are beneficial.

    Properties
  2. #2
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very easy one to explain.

    Imagine you have some functionality in your program, say a private List<string> that holds a random set of special data, and you want to give access to only 1 piece of that List<string>.

    You could easily just do:
    Code:
    public string MyString { get { return myList[0]; } }
    That is a common usage scenario. Keep in mind, anything you can normally put in a method (i.e: GetMyString() you can do in a property instead.

    Another very useful thing about properties, is the encapsulation they provide.

    Imagine the following code:
    Code:
    		private string randomName = string.Empty;
    		public string RandomName
    		{
    			get
    			{
    				if (string.IsNullOrEmpty(randomName))
    				{
    					throw new NullReferenceException("Random name is null or empty!");
    				}
    				return randomName;
    			}
    			set
    			{
    				if (randomName != value)
    				{
    					if (!string.IsNullOrEmpty(value)
    					{
    						randomName = value;
    					}
    				}
    			}
    		}
    This allows methods within your class to act on the randomName field no matter what is contained in it. While only allowing methods outside of the class to pass valid info, and throw an error when it's trying to retrieve a null or empty string. When using the following:
    Code:
    myClassInstance.RanomName = "";
    It will not change randomName as it has not passed the checks in the setter of the RandomName property. (Check if the value passed in is not the same as what is already set, and make sure the string is not null or empty)

    If you were to retrieve RandomName without ever changing the randomName field from string.Empty, you would have an exception thrown.

    The possibilities are endless. (You can let properties set certain settings throughout your program, do many different checks, etc.)

    The most common usage is just
    Code:
    public string RandomName { get; set; }
    Which acts just like a field would, but with a different intellisense glyph. (The compiler adds the backing field for you) Properties can be very, very powerful.

    For instance:

    Code:
    public string RandomName { get; protected set; }
    This would allow any other class to get the data from the RandomName property, but only classes that are derived, or are, the current class.

    Code:
    public class MyClass
    {
    	public string RandomName { get; protected set; }
    	
    	void Set()
    	{
    		RandomName = "This works!";
    	}
    }
    
    public class OtherClass
    {
    	void Set()
    	{
    		(new MyClass()).RandomName = "This will not work! Cannot set a protected property if this class does not derive from MyClass!";
    	}
    }
    
    public class MyDerivedClass : MyClass
    {
    	void Set()
    	{
    		RandomName = "This works too! We're derived from MyClass, and thus have 'protected' privileges";
    	}
    }
    The same can be done with getters.
    Code:
    public class MyClass
    {
    	// Allow only derived types to set this property. All others can get it.
    	public string RandomName { get; protected set; }
    
    	// Allow only derived types to get this property. All others can set it.
    	public string OtherName { protected get; set; }
    
    	// Allow only class instance types to get this property, all others can set it.
    	public string AnotherName { private get; set; }
    
    	// Allow only assembly internal types to get this property. Allow only class instance types can set it.
    	public string ConfusingName { internal get; private set; }
    
    	// Same as above. Just a bit less confusing.
    	internal string ConfusingName2 { get; private set; } 
    }
    Hope you understand it now.
    Last edited by Apoc; 01-18-2009 at 09:38 PM.

  3. #3
    Cephalopod's Avatar Member
    Reputation
    5
    Join Date
    Sep 2008
    Posts
    85
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I certainly do.

    Thanks for that, most of the sites I found were just bitching about whether fields should exist at all, not actually explaining uses for properties. Now I am 6 chapters in and I've finally learnt something

    Rep for you.

  4. #4
    cygnusX's Avatar Member
    Reputation
    3
    Join Date
    Feb 2009
    Posts
    66
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good explanation

  5. #5
    suicidity's Avatar Contributor
    Reputation
    207
    Join Date
    Oct 2006
    Posts
    1,439
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also use it in my Structures for entities / player to return a value but not be able to set it.. something like the following..

    Code:
    public class Player
    {
        public int Health { get { return Memory.ReadInt32(ADDRESS); } }
    }
    It allows to execute and return a value when called and you cannot write to it, Simple and nice.
    Last edited by suicidity; 02-22-2009 at 11:42 PM.


Similar Threads

  1. [Selling] Crafting 6 Property Exalted Grand Doomcastor
    By hackersunited in forum Diablo 3 Buy Sell Trade
    Replies: 0
    Last Post: 06-07-2012, 04:47 AM
  2. [Buying] Blacksmith Plan + 6 Properties
    By aminios in forum Diablo 3 Buy Sell Trade
    Replies: 2
    Last Post: 06-05-2012, 07:18 PM
  3. Are you a virtual property thief? academic anonymous survey
    By deakinresearch in forum Community Chat
    Replies: 2
    Last Post: 09-14-2011, 11:16 PM
  4. Property cache
    By Kryso in forum WoW Memory Editing
    Replies: 2
    Last Post: 12-20-2009, 02:42 PM
  5. [Info] Item properties list
    By javis in forum Age of Conan Exploits|Hacks
    Replies: 0
    Last Post: 06-07-2008, 03:40 PM
All times are GMT -5. The time now is 04:05 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search