Learn to program in Visual Basic .NET, the right way

Classes - Properties

What is a Property?

A Property is a special type of variable for a Class.

Properties will generally represent a characteristic of the Class and can optionally be read-only so that the value cannot change outside of the Class.

Example

There are two methods of creating a property.

The first method is to fully type out the property to include the getter (an inline method to get the Property's value) and the setter (an inline method to set the Property's value). The following demonstrates how to fully type out a property:

Fiddle: Live Demo

In this particular example, the property will return and set the value of the private variable _bar.

The second method is to use the auto-implemented property; this allows you to omit the getter and setter. The following demonstrates how to create an auto-implemented property:

Fiddle: Live Demo

Read-Only

To create a read-only property, create a fully typed out property, but include the ReadOnly keyword and omit the setter. The following demonstrates how to create a Read-Only property:

Fiddle: Live Demo