Home
VB.NET
Property Examples (Get, Set)
This page was last reviewed on Feb 18, 2022.
Dot Net Perls
Property. In VB.NET, 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.
Shows a property
Property notes. 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.
First example. 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.
Step 1 When the value 1 is assigned to the Number property, Set is executed. The count field stores the value 1.
Step 2 When the Number property is accessed but not assigned to, Get is executed. The value of the count field is returned.
Tip Additional logic can be inserted in either Get or Set. This extra layer of indirection is often used to validate arguments.
Shows a property
Class Example Private _count As Integer Public Property Number() As Integer Get Return _count End Get Set(ByVal value As Integer) _count = value End Set End Property End Class Module Module1 Sub Main() Dim item As Example = New Example() ' Step 1: set property. item.Number = 4 ' Step 2: get property. Console.WriteLine("NUMBER: {0}", item.Number) End Sub End Module
NUMBER: 4
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.
Module Module1 Property Answer() As Integer Get Return 42 End Get End Property Sub Main() ' Program won't compile. End Sub End Module
Error BC30124 Property without a 'ReadOnly' or 'WriteOnly' specifier must provide both a 'Get' and a 'Set'.
ReadOnly. 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.
Detail The Count() property returns a constant Integer. But Get could perform any calculation or return the value of a field.
Warning If we try to assign a value to Count, we get this error: "Property Count is ReadOnly." So don't do that.
Class Example Public ReadOnly Property Count() As Integer Get Return 500 End Get End Property End Class Module Module1 Sub Main() Dim e As Example = New Example() Console.WriteLine(e.Count) End Sub End Module
500
WriteOnly. 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.
Class Example Dim _id As Integer Public WriteOnly Property Id Set(value) ' Sets the field from an external call. _id = value End Set End Property Public Sub Display() Console.WriteLine(_id) End Sub End Class Module Module1 Sub Main() ' Create Example and assign Id. Dim e As Example = New Example e.Id = 100 e.Display() End Sub End Module
100
Automatic. 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.
Detail This is a String property. We make it Public. It contains the name of the Dog objects created.
Detail This contains the weight of the dog. We can assign it, but it has a default value of 10.
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. Console.WriteLine(dog.Name) Console.WriteLine(dog.Weight) End Sub End Module
Old Yeller 50
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.
Performance, methods. Properties are not magic. Instead they are implemented in the IL by generated methods. These have the property name prefixed with "get_" or "set_."
Note Properties can be inlined by the JIT. For setters and getters, a property will not normally cause any performance loss.
Insert. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 18, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.