VB.NET Property

Array Class Collections File String Cast Console Data Directive Enum Exception If Interface Lambda LINQ Loop Nothing Number Process Property Regex Select Sort StringBuilder Structure Sub Time Windows


A Property is similar to a Function.
With a getter
and a setter,
it controls access to a value. This value is called a backing store. With Get it returns a value. With Set it stores a value.

Get, Set

This example program uses the Property keyword.
On the Number() property,
we provide a Get block
and a Set block. In Get we return a value—the backing store _count. In Set we receive a parameter and then store it in the _count field.


Types:A property can have any data type.
It does not need to be an Integer.
It can be a Class.

And:Additional logic can be inserted in either Get or Set. This extra layer of indirection is often used to validate arguments.

Program that uses property syntax: VB.NET

Class Example
    Private _count As Integer

    Public Property Number() As Integer
	    Return _count
	End Get
	Set(ByVal value As Integer)
	    _count = value
	End Set
    End Property
End Class

Module Module1
    Sub Main()
	Dim e As Example = New Example()
	' Set property.
	e.Number = 1
	' Get property.
    End Sub
End Module



When the value 1 is assigned to the Number property, Set is executed. The _count field stores the value 1. When the Number property is accessed but not assigned to, Get is executed. The value of the _count field is returned.

Tip:A property must have both Get and Set members. It is possible to use the ReadOnly or WriteOnly keywords to eliminate this requirement.


Framework: NET

Some properties in the .NET Framework are not meant to be assigned. For example the Count property on collections is not mutable. The ReadOnly modifier changes the Property type to only have a Get method.

Return keyword

In this program, the Count() property returns a constant Integer. But Get could perform any calculation and return the value of a field. ReadOnly does not require a constant return value.

Caution:If we try to assign a value to Count, we get this error: "Property Count is ReadOnly." So don't do that.

Program that uses ReadOnly Property: VB.NET

Class Example
    Public ReadOnly Property Count() As Integer
	    Return 500
	End Get
    End Property
End Class

Module Module1
    Sub Main()
	Dim e As Example = New Example()
    End Sub
End Module





Auto-implemented properties use a shorter syntax form.
With them,
we do not specify the Get
or Set methods. We just the Property keyword and the declaration statement. We can specify a default value.

Name:This is a String property.
We make it Public.
It contains the name of the Dog objects created.


Weight:This contains the weight of the dog.
We can assign it, but it has a default value of 10.

Program, auto-implemented properties: VB.NET

Class Dog
    Public Property Name As String
    Public Property Weight As Integer = 10
End Class

Module Module1

    Sub Main()
	' Use automatically-implemented properties.
	Dim dog As Dog = New Dog()
	dog.Name = "Old Yeller"
	dog.Weight = 50

	' Print values.
    End Sub

End Module


Old Yeller

Limitations. The automatic property syntax in VB.NET is limited. We cannot use this syntax form if we want to insert code in the getter or setter. We must expand the property to do this.

However, there are situations in which you cannot use an auto-implemented property and must instead use standard, or expanded, property syntax.

Auto-Implemented Properties: MSDN


Property in Visual Studio

The Visual Studio editor provides some shortcuts for inserting properties. Try typing "pro" and pressing tab twice. Some code for a property will appear.
To change the fields,
please tab to them,
and type something.


The VB.NET programming language

Properties simplify syntax of VB.NET programs. And they provide a facility in the language that enforces consistency. We avoid writing Get() and Set() methods. Instead we use properties.