Object-Oriented Programming with Visual Basic .NET

only for RuBoard

In traditional OOP languages like C++, it is considered good practice to provide accessor methods to manipulate an object's state. For instance, if an object has a member variable named weight , a programmer often writes two functions. One function is used to get the value of weight , and another to set the valuesomething akin to get_Weight and set_Weight . To provide read-only access, the programmer would forego the get_ method .

Properties in VB.NET are merely a language construct used to provide this frequently used functionality and enforce good programming practice through the language.

Whenever you create a property, VB.NET automatically generates a get_ and set_ method behind the scenes. Essentially, a property is nothing more than language convenience. Check it out with ILDASM.

Properties can be ReadOnly , WriteOnly , or both. Read-only properties contain a Get block that allows retrieval of a value, but prevents it from being changed:

Public ReadOnly Property Age( ) As Double Get Return Me.age End Get End Property

Write-only properties use a Set block, which allows values to be initialized but not retrieved:

Public WriteOnly Property Age( ) As Double Set 'age is a Private member variable Me.age = value End Set End Property

Properties that provide both read and write access must specify only an access modifier:

Public Property Age( ) As Double Get Return Me.age End Get Set (ByVal value As Double) Me.age = value End Set End Property

In the Set block, you can specify the name and type of the incoming value. If you choose not to, the compiler uses "value" as the placeholder for the property value.

3.2.1 Read-only fields

The ReadOnly keyword can also be applied to member data. These fields can be initialized during object construction, but not after. Usually, member data, like the private member age from Example 3-2, is accessed through a property. But access to it can be tedious when you just want to provide access to a read-only value that does not need processing before you return it to the caller.

Public Class World Public ReadOnly age As Double End Class

3.2.1.1 Default properties

Default properties allow array-like access to a class. These properties are also known as indexers and are very useful for implementing collections. In Example 3-3, a collection of Planet objects is created and then iterated. You do not need to refer to the default property ( Item ) by name. The collection class can be used like an array. You can see its use in the For...Next loop at the bottom of Example 3-3.

Example 3-3. Default properties

Imports System Imports System.Collections Public Class Planet Public ReadOnly Name As String Public Sub New(ByVal name As String) Me.Name = name End Sub End Class Public Class Planets Private myPlanets As New ArrayList( ) Default Public ReadOnly Property Item(ByVal idx As Integer) As Planet Get 'Convert object in array list to Car and return Return CType(myPlanets.Item(idx), Planet) End Get End Property Public ReadOnly Property Count( ) As Integer Get Return myPlanets.Count End Get End Property Public Sub Add(ByVal value As Planet) myPlanets.Add(value) End Sub End Class Public Class App Public Shared Sub Main( ) Dim i As Integer Dim myPlanets As New Planets( ) Dim p1 As New Planet("Neptune") Dim p2 As New Planet("Venus") Dim p3 As New Planet("Pluto") 'Add 3 planets to collection myPlanets.Add(p1) myPlanets.Add(p2) myPlanets.Add(p3) For i = 0 To myPlanets.Count - 1 Console.WriteLine(myPlanets(i).Name) Next i End Sub End Class

only for RuBoard

Категории