Skip to content
Classes and Properties
step 1/5

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 Type is auto-property — generates getter/setter.
  • Me is the instance reference (like this).
  • Sub New(...) is the constructor.
  • Function ... As Type returns; Sub is void.

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…