Skip to content
Lesson 1 of 7

Step 1 of 5 · Reading · ~2 min

Learn

Classes and Properties

VB.Net classes are full .NET classes — same runtime as C#.

Class Person
    Private _name As String
    Private _age As Integer

    Public Sub New(name As String, age As Integer)
        Me._name = name
        Me._age = age
    End Sub

    Public Property Name As String
        Get
            Return _name
        End Get
        Set(value As String)
            _name = value
        End Set
    End Property

    Public ReadOnly Property Age As Integer
        Get
            Return _age
        End Get
    End Property

    Public Function Greeting() As String
        Return "Hi, I'm " & Name & " and I'm " & Age.ToString()
    End Function
End Class
  • A property is a pair of methods wearing the costume of a field: Get runs on read, Set on write, and callers never see the difference.
  • Value is the implicit parameter of Set — the value being assigned.
  • Me is the instance reference (C#'s this).
  • Sub New(...) is the constructor; Function ... As Type returns, Sub does not.
  • ReadOnly Property has a Get and no Set: assignable from the constructor's own field, never from outside.

Why a property and not a public field? Because the costume is the point. You can add validation, logging or laziness inside Set later without any caller changing a line — and a public field can never grow that ability without breaking every assembly compiled against it.

Computed properties hold no field at all:

Public ReadOnly Property FullName As String
    Get
        Return _firstName & " " & _lastName
    End Get
End Property

Asymmetric access — public to read, private to write. This is the single most useful property shape in a class you intend other people to use:

Private _count As Integer

Public Property Count As Integer
    Get
        Return _count
    End Get
    Private Set(value As Integer)
        _count = value
    End Set
End Property

Outside code can read Count and cannot corrupt it; the class's own methods still assign it freely. Private Set narrows the setter only — the property itself stays public.

Object initializer syntax sets properties at construction time:

Dim p As Person = New Person("Ada", 36)
p.Name = "Ada Lovelace"

On a modern compiler you would also have New Person With {.Name = "Ada"} and Public Property Name As String as a one-liner. Read on for why neither compiles in this course's editor.

What this course's compiler accepts

The grader runs vbnc, the Mono VB compiler frozen at the VB 9/10 language level. Three modern conveniences are not available to you in the editor:

  • auto-implemented properties (Public Property Name As String with no body) — write the full Get/Set block over a private backing field;
  • string interpolation ($"...") — build strings with & or String.Format;
  • lambdas and LINQ (Function(n) ..., From n In xs) — use explicit loops.

They are all real VB.NET and you will use them on a current toolchain; they simply cannot be compiled here. Every example below that uses one is marked.

Up nextInheritanceClasses and Properties

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…