Instance Fields


One of the key purposes of object-oriented design is the grouping of data to provide structure. This section discusses how to add data to the Employee class. The general object-oriented term for a variable that stores data within a class is member variable. This term is well understood in C#, but the more accurate term is field, which is a named unit of storage associated with the containing type. Instance fields are variables declared at the class level to store data associated with an object. Hence, association is the relationship between the field data type and the containing field.

Declaring an Instance Field

In Listing 5.4, Employee has been modified to include three fields: FirstName, LastName, and Salary.

Listing 5.4. Declaring Fields

 class Employee {   public string FirstName;   public string LastName;   public string Salary; } 

With these fields added, it is possible to store some fundamental data with every Employee instance. In this case, you prefix the fields with an access modifier of public. public on a field indicates that the data within the field is accessible from classes other than Employee (see the section Access Modifiers, later in this chapter).

As with local variable declarations, a field declaration includes the data type to which the field refers. Furthermore, it is possible to assign fields an initial value at declaration time, as demonstrated with the Salary field in Listing 5.5.

Listing 5.5. Setting Initial Values of Fields at Declaration Time

 class Employee {   public string FirstName;   public string LastName;   public string Salary = "Not enough";                          } 

Accessing an Instance Field

You can set and retrieve the data within fields. However, the fact that the field does not include a static modifier indicates that it is an instance field. You can access an instance field only from an instance of the containing class (an object). You cannot access it from the class directly (without first creating an instance, in other words).

Listing 5.6 shows an updated look at the Program class and its utilization of the Employee class, and Output 5.1 shows the results.

Listing 5.6. Accessing Fields

 class Program {   static void Main()   {       Employee employee1 = new Employee();       Employee employee2;       employee2 = new Employee();       employee1.FirstName = "Inigo";                          employee1.LastName = "Montoya";                         employee1.Salary = "Too Little";                        IncreaseSalary(employee1);                              Console.WriteLine(                                          "{0} {1}: {2}",                                         employee1.FirstName,                                    employee1.LastName,                                     employee1.Salary);                                  // ...   }   static void IncreaseSalary(Employee employee)   {       employee.Salary = "Enough to survive on";                 } } 

Output 5.1.

 Inigo Montoya: Enough to survive on 

Listing 5.6 instantiates two Employee objects, as you saw before. Next, it sets each field, calls IncreaseSalary() to change the salary, and then displays each field associated with the object referenced by employee1.

Notice that you first have to specify which Employee instance you are working with. Therefore, the employee1 variable appears as a prefix to the field name when assigning and accessing the field.

Regardless of whether fields are assigned at declaration time or within code later on, the C# compiler requires that an instance field be definitely assigned somewhere within the execution path prior to accessing the field. Using static code analysis, the C# compiler will report errors when instance variables are not assigned, which is sometimes rather subtle. For example, calling if(employee1.Salary == "Too Little"){...} before assigning a value to employee1.Salary will cause the compiler to report an error.




Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net