Theme index -- Keyboard shortcut: 'u'  Previous theme in this lecture -- Keyboard shortcut: 'p'  Next slide in this lecture -- Keyboard shortcut: 'n'Data Access, Properties, and Methods

A complete PDF version of the text book is now available. The PDF version is an almost complete subset of the HTML version (where only a few, long program listings have been removed). See here.

17.  Accessing Data in Objects

This is the start of the lectures about data access and operations.

In this and the following sections we will discuss the various operations in classes, in particular how to access data which is encapsulated in objects. By data access we mean both reading (getting) and writing (setting).

In this material we use the word operation as a conceptual term. There is nothing called an "operations" in C#. Rather, there are methods, properties, indexers, operators etc. Thus, when we in this teaching material use the word operation it covers - in a broad sense - methods, properties, indexers, operators, events, delegates, and lambda expressions.

17.1 Indirect data access17.2 Overview of data access in C#
 

17.1.  Indirect data access
Contents   Up Previous Next   Slide Annotated slide Aggregated slides    Subject index Program index Exercise index 

It is not a good idea to access the instance variables of a class directly from client classes. We have already discussed this issue in relatively great details in Section 11.3 and Section 11.5.

Data encapsulated in a class is not accessed directly from other classes

Rather, data is accessed indirectly via operations

So the issue is indirect data access instead of direct data access, when we work on a class from the outside (from other classes, the client classes). When we use indirect data access, the data are accessed through some procedure or function. This procedure or function serves as the indirection in between the client, which makes use of the data, and the actual data in the class. This "place of indirection" allows us to carry out checks and other actions in the slipstream of data access. In addition, the procedures and functions that serve as indirection, makes it possible to program certain compensations if the data representation is modified at a later point in time. With this, the client classes of a class C are more likely to survive future modifications of the data, which C encapsulates. It is possible to calculate the data instead of accessing it from variables in the memory.

The following summarizes why indirect data access is better than direct data access.

  • Protects and shields the data

    • Possible to check certain conditions each time the data is accessed

    • Possible to carry out certain actions each time the data is accessed

  • Makes it easier - in the future - to change the data representation

    • Via "compensations" programmed in the accessing operations

  • Makes it possible to avoid the allocation of storage for some data

    • Calculating instead of storing

Protection and shielding may cause the data to be accessed in a conditional data structure. One particular shielding is provided by the precondition of an operation. If the condition does not hold, we may choose not to access the data. In this material, we discuss preconditions in the context of contracts in Chapter 50.

In some circumstances it may be convenient to carry out some action whenever the data of a class is accessed. This is probably most relevant when the data in the class is mutated (assigned to new values). Data values, for which we activate a procedure upon data access, are sometimes called active values.

Accessor compensation as a remedy of changing the representation of data is undoubtedly the most important issue. We illustrated this issue in the modifications of Program 11.2, as suggested in Exercise 3.3. In a nutshell, we can often fix the consequences of a shift in data representation by modifying the internals of the operations. By keeping the class interface unchanged all the direct and indirect clients of the affected class will survive. No changes are needed in the client classes. This is the effect of firewalls, as already discussed in Section 11.5.

 

17.2.  Overview of data access in C#
Contents   Up Previous Next   Slide Annotated slide Aggregated slides    Subject index Program index Exercise index 

Below we summarize the various kinds of data access, as supported by operations in C#:

  • Directly via public instance variables

    • Never do that!

  • Indirectly via properties

    • Clients cannot tell the difference between access via properties and direct access of instance variables

  • Indirectly via methods

    • Should be reserved for "calculations on objects"

  • Indirectly via indexers

    • Provides access by means of the notation known from traditional array indexing

  • Indirectly via overloaded operators

    • Provides access by means of the language-defined operator symbols

In the next chapter we will discuss properties in C#. Chapter 19 is about indexers. Methods will be discussed next in Chapter 20. Overloaded operators are treated in Chapter 21.

Generated: Monday February 7, 2011, 12:15:31
Theme index -- Keyboard shortcut: 'u'  Previous theme in this lecture -- Keyboard shortcut: 'p'  Next slide in this lecture -- Keyboard shortcut: 'n'