This documentation is archived and is not being maintained.

Properties and Property Procedures

You can use both properties and fields to store information in an object. Whereas fields are simply public variables, properties use property procedures to control how values are set or returned. Property procedures are blocks of code declared within property definitions that allow you to execute code when a property value is set or retrieved. Visual Basic .NET has two types of property procedures: Get property procedures for retrieving a property value, and Set property procedures for assigning a value to a property. For example, a property that stores the balance of a bank account might use code in a Get property procedure to post interest and check for service fees before returning the available balance. The Set property procedure for the available balance could provide validation code to prevent the balance from being updated incorrectly. In short, property procedures allow an object to protect and validate its own data.

The following code fragment compares how fields and properties validate values:

Protected Sub TestFieldsAndProperties()
   ' Assume, for this example, that the only valid values for
   ' the field and property are numbers less than 10.
   Dim NewClass As New ThisClass()
  
   ' Test data validation. 
   NewClass.ThisField = 36   ' Works because there is no data validation.
   MsgBox("ThisField = " & NewClass.ThisField)   ' Will print 36.

   ' The attempt to set the field to a value greater than 10 will silently fail.
   NewClass.ThisProperty = 36
   ' The next statement will print the old value of 0 instead.
   MsgBox("ThisProperty = " & NewClass.ThisProperty)
End Sub

Public Class ThisClass
   Public ThisField As Integer   ' This is a field.
   Private m_PropertyValue   As Integer = 0   ' Used for Property Set operations.
   Public Property ThisProperty() As Integer   ' This is a property.
      Get
         Return m_PropertyValue  
      End Get
      Set(ByVal Value As Integer)
         ' Only allow Set operation for values less than 10.
         If Value < 10 Then m_PropertyValue = Value
      End Set
   End Property
End Class

The procedure TestFieldsAndProperties creates an instance of the class and sets and retrieves the values of fields and properties. For the sake of this example, it is assumed that the only valid values are numbers less than 10. Because there is no way to validate values assigned to fields, setting the field to value 36 is allowed. The property, however, only performs assignment for numbers less than 10, so the attempt to set it to 36 is ignored.

Read-Only and Write-Only Properties

Most properties have both Get and Set property procedures to allow you to both read and modify the value stored inside. However, you can use the ReadOnly or WriteOnly modifiers to restrict properties from being modified or read. Read-only properties cannot have Set property procedures; they are useful for items that you want to expose but not allow to be modified. For example, you could use a read-only property to provide the processor speed of a computer. Write-only properties cannot have Get property procedures and are useful for data that you want to store but not expose to other objects. For example, a write-only property could be used to store a password.

Note   Previous versions of Visual Basic support Let property procedures for use when assigning objects to properties. Visual Basic .NET eliminates the need for Let property procedures because object assignment is handled like any other kind of assignment.

See Also

Get Statement | Set Statement | Property Procedures | Property Procedures vs. Fields | ReadOnly | WriteOnly

Show: