Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Instance Fields

One of the key aspects 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 standard term and the one used in the specification 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, the class 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. The use of 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";                            
}

We delay the guidelines of naming and coding fields until later in the chapter, after C# properties have been introduced. Suffice it to say, Listing 5.5 does not follow the general convention.

Accessing an Instance Field

You can set and retrieve the data within fields. However, the fact that a 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.

  • + Share This
  • 🔖 Save To Your Account