Implementing Properties in dot net

Properties are members of classes that expose member variables or objects. Properties have similarities to both fields and methods. Values are set and retrieved using the same syntax as fields: getting a value from a property or setting a value with a property actually calls a specialized method that carries out these functions. Properties can contain code that validates values before setting them or carries out any other function that is required by the application.

Properties allow you to expose member values and objects in a more robust way than simply using fields. A property is essentially a specialized method that looks like a field. Property values are set and retrieved in the same manner as fields.

The Value (value) keyword is a special keyword used in the setter of a property. It always represents the value to which the property is being set. Properties can expose any kind of object, array, or collection. The value or object is usually stored in a private local variable (mText in the preceding example) and retrieved when the property is accessed.

The preceding example is a fairly trivial example of a property because it behaves in essentially the same manner as a field. One of the key advantages of properties is that you can provide additional code in the getter and setter of the property to perform calculations or validate the entry. The example below demonstrates how to create a property where the value is validated before being set.

To create a property

  1. Create a private member field or object to hold the value or object that will be returned by the property.
  2. Write the code for the property that will return the value.
  3. Add any validation code in the getter or the setter that is required for the property.
  4. Read-Only and Write-Only Properties

Sometimes you will need to implement a property that can return a value to the client but cannot be changed once the class is initialized. Very infrequently, you might need to create a property that can be changed but not read. These properties are called read-only and write-only properties, respectively.

Creating Read-Only Properties

Read-only properties are created in a manner similar to regular properties. In Visual C#, creating a read-only property is as simple as providing only a getter and omitting the setter from a property. In Visual Basic .NET, the property must be marked with the ReadOnly keyword, and the setter is omitted. In both languages, the private variable that holds the property value must be marked with the ReadOnly (readonly) keyword.

Because the variable holding the data for the property is read-only, it cannot be set or changed in code. You must set an initial value for the variable in the constructor of the class, or when the variable is initialized.

To create a read-only property

  1. Create a private member variable to hold the property value. This variable must be marked ReadOnly in Visual Basic .NET and readonly in Visual C#.
  2. Implement the property, supplying a getter but not a setter. In Visual Basic .NET, the property should also be marked ReadOnly.
  3. Set the value of the private member variable in the constructor of the class for which it is a member.

Write-Only Properties

Although used very infrequently, it is possible to create properties that can be written to but not read by the client. You might implement a write-only property that controls localization properties of a form. When a different locale is set, code in the property would execute to make the appropriate changes to the form, and there would be no need to access the value of the property.

A write-only property is created in a manner analogous to a read-only property. If the value of the write-only property is to be stored, you will need to create a private member variable to hold it. The property itself is implemented as a regular property, but only a setter is supplied. The getter is omitted. In Visual Basic .NET, the property must be marked with the WriteOnly keyword.

To create a write-only property

If needed, create a private member variable to hold the value of the write-only property.

Implement your property supplying only a setter. In the setter, write any code that you want to execute when the property is set. In Visual Basic .NET, you must also mark the property with the WriteOnly keyword.

Parameterized Properties

Most of the properties you create will return a single value or object. In Visual Basic .NET, you can create properties that accept parameters when accessed. These properties usually expose a range, or array of values. For example, a single Engine object might have several Cylinders. Rather than expose each one individually, you could create a Cylinder property that returned or set each cylinder based on a supplied parameter.

You implement a parameterized property just as you would a normal property except that in the property declaration, you declare the parameter. Then, in the getter and setter of the property, you write appropriate code to retrieve the appropriate value or object.

Default Properties and Indexers

In Visual Basic .NET, you can designate a default property for your component. A default property is always a parameterized property and is usually used to expose a range, array, or collection of values. Enabling a default property only makes sense if the primary purpose of the object is to contain and expose other objects. If you mark a property as default, you can use the property name interchangeably with the object name.

The equivalent of a Visual Basic .NET default property in Visual C# is the indexer. The indexer is a specialized property that allows you to expose a group of objects on the name of the object. The name used in the code of the indexer is always this, which indicates that the name of the object itself will be used to access the property.

To create a default property in Visual Basic .NET

Create a parameterized property that returns an object based on the parameter. Use the Default keyword in the property declaration.

To create an indexer in Visual C#

Create a property named this, and designate an index variable for the property. Write code that returns the appropriate object based on the index.

Collection Properties

If your object exposes an undetermined number of objects of the same type, you can expose a collection of objects through a property. Exposing collections of objects as properties allows you to control access to your subordinate objects and validate assignments.

There are a few different ways to implement collection properties. How you choose to do so depends on the kind of functionality you need to implement. Simple collection properties can simply return the collection. This allows clients to add and remove members to the collection as well as use all of the collection's methods.

You would probably expose the collection as a read-only property because you would never want to allow the client to assign the property to a new instance of the collection class itself. The objects in the collection could still be changed with the collection's own methods.

Although this approach is simple and allows you access to the collection's members, there is one fairly significant problem. Most of the collection classes in the System.Collections namespace accept and return their members as objects. This means that any kind of type can be added or removed to the collection. Thus, there is no type checking when members are added to the collection, and retrieved members must be explicitly converted to the correct type. If you want to manage groups of differently typed objects in a single property, this approach will be adequate.

To create a simple collection property

Implement a read-only property that returns the collection.

For more robust collection properties, you should wrap your collection object in the property and provide explicit conversion and validation code within the property. This allows you to access the collection members by index and control the types that are added to the collection. In Visual Basic .NET, you must implement a property of this type as a parameterized property.

This approach allows for properties that are strongly typed and exposes a collection of like-typed items. There are some drawbacks to this approach, however. You lose the ability to iterate through the collection using For Each...Next (foreach), and any other methods of the property that you want to expose to the client must be wrapped as well. Additionally, the syntax concessions required in Visual C# might also be unsuitable.

To create a strongly typed collection property

In Visual Basic .NET, create a parameterized property that returns a member of a private collection based on the index. Use the CType keyword to convert the object returned by the collection to the appropriate type.

In Visual C#, create a pair of methods that get and set the object at the appropriate index in a private collection. In the get method, explicitly convert the object returned by the collection to the appropriate type.

For properties that require an even stronger implementation, you can implement your own strongly typed collection by inheriting from the ystem.Collections.CollectionBase class. This class provides most of the background functionality required by collections and allows you to provide your own implementations of the item property.

With a strongly typed collection, you can expose the collection directly in the property (as shown in the preceding example) and still have access to all the collection functionality. The collection will only accept members of the type you specify and will incorporate any other functionality you require.

related post











No comments:

Post a Comment