R N REDDY .Net Community - C#
Sunday, 2016-12-04, 11:52 PM
Welcome Guest | RSS
Site menu
Tag Board
500
Our poll
How is .Net classes by R.N.Reddy sir ?
Total of answers: 230
Statistics

Total online: 1
Guests: 1
Users: 0


30-12-2010

Read only:


As per our requirement we have to represent a class employee. Within this class we have to represent field called EmpNo.

EmpNo value we can assign to the employee only after he joined and the EmpNo is a fixed value.

Here we cannot initialize value in design time we have to initialize in run time & the value should not be changed.

Lets declare EmpNo field as a variable.

namespace Company2
 {
    Public class Employee
        {
    Public int empno;
    Public employee()
    {
      empno = getempno();
    }
    Public getempno()
    {
//assuming that empno we are retrieving from database
    return 11;
    }
  }

   Class program
   {
      Static void main()
         {
            Employee john = new employee();
            Console.WriteLine("John empno is " +john.empno);
            john.empno = 222;
            Console.WriteLine("john empno is " +john.empno);
            Console.ReadLine();
         }
   } 
}
Output: john empno is 11
             john empno is 22

In the above program empno field we have declared as a variable. With the variable we are able to initialize the value in run time, but it is allowing to change the empno value.

But empno is a fixed value once we initialized it should not be changed due to that reason for our above requirement variable is not suitable.

Lets try declare empno field as a constant.

According to constant concept it should be initialized at the time of declaration but we have to initialize empno field at the time of run-time due to that reason constant is not suitable for our requirement.

Lets try to declare empno field as read only.

To declare a read only field we use read only keyword.

Read only field we can initialize at the time of declaration that is in design time as well as we can initialize in run-time, the convenient place to initialize in run-time is within the constructor.

Whenever we want to initialize a value in design time which cannot be changed then we can declare that value as constant.

Whenever we want to initialize a value in run time which cannot be changed then we can declare that value as read only.

Read only is by default a non-static members

namespace Company2
 {
   Public class Employee
       {
         Public readonly int empno;
         Public employee()
           {
              empno = getempno();
           }
         Public int getempno()
         {
//assuming that empno we are retrieving from database
            return 111;
          }
       }

   Class program
    {
       void main()
         {
            Employee john = new employee();
            Console.WriteLine("John empno is " +john.empno);
            Console.ReadLine();
         }
   }
}
 john empno is 111

Difference between Constant & Read only:


OOPS Concepts:

    1. Class & Object
    2 . Encapsulation
    3. Abstraction
    4. Inheritance
    5. Polymorphism


2. Encapsulation: Keeping data & member function into a single unit can be called as Encapsulation.

Binding the fields, properties & members functions into a single unit called class is a called as encapsulation.

How to encapsulate ?

    By implementing classes & structures we can achieve encapsulation.

3. Abstraction: Hidding the data & member function's from outside classes can be called as abstraction.

How to Abstract ?
    By implementing access modifiers like private, protected, internal and protected internal we can achieve abstraction.





Search

Time
 

 

Calendar
 
«  December 2016  »
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
25262728293031

Twitter
 

Entries archive

Copyright R N Reddy © 2016Developed by Nagendra, Venkat, Vijaya and Yaswanth
Get free updates as SMS to your mobile. Just register by clicking here