Reading — step 1 of 5
Learn
~1 min readClasses and Properties
VB.Net classes are full .NET classes — same runtime as C#.
Class Person
Public Property Name As String
Public Property Age As Integer
Public Sub New(name As String, age As Integer)
Me.Name = name
Me.Age = age
End Sub
Public Function Greeting() As String
Return $"Hi, I'm {Name} and I'm {Age}"
End Function
End Class
Public Property X As Typeis auto-property — generates getter/setter.Meis the instance reference (likethis).Sub New(...)is the constructor.Function ... As Typereturns;Subisvoid.
Computed properties:
Public ReadOnly Property FullName As String
Get
Return $"{FirstName} {LastName}"
End Get
End Property
Asymmetric access:
Public Property Count As Integer
Get
Return _count
End Get
Private Set
_count = Value
End Set
End Property
Note Value is the implicit setter parameter (like C#'s value).
Object initializer syntax:
Dim p = New Person With {.Name = "Ada", .Age = 36}
The leading . is the field selector.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…