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.
Based on: .NET 4.5 VB.NET program that uses property syntax 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 e As Example = New Example() ' Set property. e.Number = 1 ' Get property. Console.WriteLine(e.Number) End Sub End Module Output 1
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 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 Output 500
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 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 Output 100
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 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 Output Old Yeller 50
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
Note:Properties can be inlined by the JIT. For setters and getters, a property will not normally cause any performance loss.
Tip:To change the fields, please tab to them. Then enter the desired identifier or type.