Thursday 7 March 2013

Properties

There are also members of a class using which we can provide access to the value of a class outside of the class.
If at all a class is associated with any values and if we want those values to be accessible outside of the class we can provide access to those values in two different ways.
  • By storing value under a public variable we can provide access to that value outside the class but in this case the public variable gives get value or set the value.
       public class Test
       {
       public int x=100;
       }
       Test obj=new Test()
       int a=obj x
       obj.x=100
  • By storing the value under a private value also of a class by defining a property on that variable given in three different ways
  1. Both get and set access(Read/write/property)
  2. Only get access(Read only property)
  3. Only set access(write only property)
Syntax to define a property:

      [<modifiers>]<type>[Names]
      {
      [get{statements}]  // Get accessor
      [set{statements}]  // Set accessor
      }