VB.NET Property Examples (Get, Set)Use the Property keyword to improve program syntax. A property gets and sets values.
Property. 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, a property returns a value. With Set it stores a value. We must have both Get and Set unless we specify ReadOnly or WriteOnly on the property.
On the Number property, we provide Get and Set blocks. 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.
Assign: When the value 1 is assigned to the Number property, Set is executed. The count field stores the value 1.
Access: When the Number property is accessed but not assigned to, Get is executed. The value of the count field is returned.
VB.NET program that uses property syntax
Private _count As Integer
Public Property Number() As Integer
Set(ByVal value As Integer)
_count = value
Dim e As Example = New Example()
' Set property.
e.Number = 1
' Get property.
Provide Get, Set. A property must have both Get and Set members. It is possible to use the ReadOnly and WriteOnly keywords to eliminate this requirement.
VB.NET program that causes compile-time error
Property Answer() As Integer
' Program won't compile.
Property without a 'ReadOnly' or 'WriteOnly' specifier must provide both a 'Get' and a 'Set'.
Some properties are not meant to be assigned. For example, "Count" on collections is not mutable. The ReadOnly modifier changes the Property type to have only a Get method.
Count: The Count() property returns a constant Integer. But Get could perform any calculation or return the value of a field.
Caution: If we try to assign a value to Count, we get this error: "Property Count is ReadOnly." So don't do that.
VB.NET program that uses ReadOnly Property
Public ReadOnly Property Count() As Integer
Dim e As Example = New Example()
Here we use the WriteOnly keyword on a property. WriteOnly means that a Property has only a Set() method and no Get method. In Main we create and assign to the Id property.
Tip: In program design, WriteOnly properties may be confusing. Often a method is a clearer way to set values.
VB.NET program that uses WriteOnly keyword
Dim _id As Integer
Public WriteOnly Property Id
' Sets the field from an external call.
_id = value
Public Sub Display()
' Create Example and assign Id.
Dim e As Example = New Example
e.Id = 100
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.
Name: This is a String property. We make it Public. It contains the name of the Dog objects created.Strings
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
Public Property Name As String
Public Property Weight As Integer = 10
' Use automatically-implemented properties.
Dim dog As Dog = New Dog()
dog.Name = "Old Yeller"
dog.Weight = 50
' Print values.
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.
Quote: 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: Microsoft
Properties are not magic. Instead they are implemented in the IL by generated methods. These have the property name prefixed with "get_" or "set_."IL
Note: Properties can be inlined by the JIT. For setters and getters, a property will not normally cause any performance loss.
The Visual Studio editor provides some shortcuts for inserting properties. Try typing "pro" and pressing tab twice. Some code for a property will appear.
Tip: To change the fields, please tab to them. Then enter the desired identifier or type.
A summary. Properties simplify syntax of VB.NET programs. And they help the language enforce consistency. We avoid writing Get() and Set() methods. Instead we use properties.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.