Home
VB.NET
Class Examples
Updated Aug 7, 2024
Dot Net Perls
Class. VB.NET programs can be complex. A class is one part of a program—it is self-contained. When we modify a class, other parts of the program are not affected.
A program in VB.NET may also contain Modules and Namespaces. But the Class is the core unit—things like Strings and Lists are special classes.
Interface
Generic
First example. This program introduces an Example Class. In the Class, we have a Private field of type Integer. We also have a constructor—the New() Sub.
Finally We have the Value() Function, which returns an expression based on a field. It is Public, so can be called from Main.
Step 1 In the Main Sub, control flow begins. We create an instance of the Example Class—an Example now exists on the managed heap.
Step 2 We access the Example instance (called "x") and invoke its Value Function. This returns 2, and we print the result.
Class Example
    Private _value As Integer

    Public Sub New()
        _value = 2
    End Sub

    Public Function Value() As Integer
        Return _value * 2
    End Function
End Class

Module Module1
    Sub Main()
        ' Step 1: create a new instance of Example Class.
        Dim x As Example = New Example()

        ' Step 2: call Value Function on the Example.
        Console.WriteLine(x.Value())
    End Sub
End Module
4
Me qualifier. With the Me qualifier, we specify a member directly on the current instance of the class. If the class is derived, any further derived classes are ignored.
Info With "Me," we can qualify whether we mean to indicate a local variable, or a member on the current class.
Here We use Me.species to reference the field on the Bird class. The variable "species" is the parameter to the New Sub.
Class Bird
    Private species As Integer

    Public Sub New(ByVal species As Integer)
        ' Print Me and not-Me variables.
        Console.WriteLine("ME: {0}/{1}", Me.species, species)

        ' The Me qualification makes it clear we are assigning a field.
        Me.species = species
    End Sub
End Class

Module Module1
    Sub Main()
        Dim bird = New Bird(10)
    End Sub
End Module
ME: 0/10
Inherits. With Inherits, one class can inherit from another class. This means it gains all the fields and procedures from the parent class.
Start Let's begin by looking at Class A: this class contains a field (_value) as well as a Sub (Display()).
Info These 2 classes use the Inherits keyword and are derived from Class A. They provide their own constructors (New).
Tip You can see the New B() and New C() will do slightly different things when called.
Tip 2 Class B and Class C inherit Display from Class A. When b.Display() and c.Display() are called, the A.Display Sub is invoked.
Important A MustInherit Class is essentially a template that is part of the classes that inherit from it.
MustInherit
Class A
    Public _value As Integer

    Public Sub Display()
        Console.WriteLine(_value)
    End Sub
End Class

Class B : Inherits A
    Public Sub New(ByVal value As Integer)
        MyBase._value = value
    End Sub
End Class

Class C : Inherits A
    Public Sub New(ByVal value As Integer)
        MyBase._value = value * 2
    End Sub
End Class

Module Module1
    Sub Main()
        Dim b As B = New B(5)
        b.Display()

        Dim c As C = New C(5)
        c.Display()
    End Sub
End Module
5 10
MyClass versus Me. The MyClass and Me qualifiers have an important difference. MyClass refers to the current class, and Me refers to the current instance—it could be called "MyInstance."
Info With MyClass, a method on the current instance of the class is called. So in the Animal class, Animal.Test is invoked.
And The "Me" keyword references the current instance, not the current class. So Cat.Test is invoked, because we have a Cat object.
Class Animal
    Public Sub Enter()
        ' Use MyClass and Me to call subroutines.
        MyClass.Test()
        Me.Test()
    End Sub

    Public Overridable Sub Test()
        Console.WriteLine("Animal.Test called")
    End Sub
End Class

Class Cat : Inherits Animal
    Public Overrides Sub Test()
        Console.WriteLine("Cat.Test called")
    End Sub
End Class

Module Module1
    Sub Main()
        Dim cat As Cat = New Cat()
        cat.Enter()
    End Sub
End Module
Animal.Test called Cat.Test called
Shared. Some fields in a Class are not tied to a Class instance. Only one instance is needed. Shared is used on fields to make one field shared among all Class instances.
Tip A Public Shared field can be used in the same way as a global variable. This is useful for storing settings.
Shared
Class Test
    Public Shared _v As Integer
End Class

Module Module1
    Sub Main()
        Test._v = 1
        Console.WriteLine(Test._v)
        Test._v = 2
        Console.WriteLine(Test._v)
    End Sub
End Module
1 2
Shared Sub. These methods are not tied to a Class instance. A Shared Sub can be called with a composite name. Next, the Write Sub inside the Test class is called with "Test.Write()".
Class Test
    Public Shared Sub Write()
        Console.WriteLine("Shared Sub called")
    End Sub
End Class

Module Module1
    Sub Main()
        Test.Write()
    End Sub
End Module
Shared Sub called
Partial. This modifier specifies that a class is specified in multiple declarations. With Partial, were open a class and add new parts to it. A Class can span multiple files.
Module Module1

    Partial Class Test
        Public Sub X()
            Console.WriteLine("X")
        End Sub
    End Class

    Partial Class Test
        Public Sub Y()
            Console.WriteLine("Y")
        End Sub
    End Class

    Sub Main()
        ' Invoke methods on the partial class.
        Dim t As Test = New Test()
        t.X()
        t.Y()
    End Sub

End Module
X Y
Friend. This modifier makes a member (like a Class, Sub or Function) unavailable outside the present assembly. An assembly is a physical file that contains compiled code.
Here We specify that Display() is a Friend Sub. So it is Public inside the assembly, but not available outside.
Class Item
    Friend Sub Display()
        Console.WriteLine("Friend Class used")
    End Sub
End Class

Module Module1
    Sub Main()
        ' The Display Sub is public if we are in the same assembly.
        Dim local As Item = New Item()
        local.Display()
    End Sub
End Module
Friend Class used
Object. All classes inherit from Object, the ultimate base class. We thus can cast all variables to an Object. The object class provides some helpful methods.
Object
Info GetHashCode() returns a hash code integer based on the content of the instance.
Class Example
    ' An empty Example class: it automatically inherits from Object.
End Class

Module Module1
    Sub Main()
        ' Create instance of the Example class.
        Dim x As Example = New Example()

        ' The class inherits from Object, so we can cast it to an Object.
        Dim y As Object = x
        ' ... This method is from Object.
        Console.WriteLine(y.GetHashCode())

        ' When we call GetHashCode, the implementation from Object is used.
        Console.WriteLine(x.GetHashCode())
    End Sub
End Module
46104728 46104728
Summary. Classes are essential building blocks. A class is a reference type: it is allocated on the managed heap. It can have Functions, Subs, and data members.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Aug 7, 2024 (simplify).
Home
Changes
© 2007-2025 Sam Allen