home comics writing pictures archive about

2022-10-29 - In IL: VB Classes, Modules and Structures

In IL

Last time we looked at Classes and structs in C#, now we will look at the Visual Basic .NET version of those concepts. This program has a variety of Class, Module and Structure definitions.

Module1.vb
Module Module1
Sub Main()
End Sub
End Module
Friend Class FriendClass
End Class
Public Class PublicClass
End Class
Public Class NestedClass
Private Class NestedPrivateClass
End Class
Private Protected Class NestedPrivateProtectedClass
End Class
Friend Class NestedFriendClass
End Class
Protected Class NestedProtectedClass
End Class
Protected Friend Class NestedProtectedFriendClass
End Class
Public Class NestedPublicClass
End Class
End Class
Public Interface IInterface
End Interface
Public MustInherit Class MustInheritClass
End Class
Public NotInheritable Class NotInheritableClass
Inherits MustInheritClass
End Class
Public Module PublicModule
End Module
Public Class DerivedClass
Inherits MustInheritClass
End Class
Public Class InterfaceClass
Implements IInterface
End Class
Public Structure PublicStructure
End Structure
Public Enum PublicEnum
None = 0
End Enum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

This program is basically the same as the C# one we looked at last time but with some terminology differences. For example we have Friend instead of internal, MustInherit instead of abstract, NotInheritable instead of sealed, and Structure instead of struct. If we look at the IL we see that these keywords are interpreted the same way.

.class private auto ansi VbClass.FriendClass
extends [mscorlib]System.Object
516
517
.class public auto ansi VbClass.PublicClass
extends [mscorlib]System.Object
531
532

As you can see FriendClass also gets marked as private the same way InternalClass was in the C# example and PublicClass still gets marked as public.

.class auto ansi nested private NestedPrivateClass
extends [mscorlib]System.Object
549
550
.class auto ansi nested famandassem NestedPrivateProtectedClass
extends [mscorlib]System.Object
564
565
.class auto ansi nested family NestedProtectedClass
extends [mscorlib]System.Object
594
595
.class auto ansi nested famorassem NestedProtectedFriendClass
extends [mscorlib]System.Object
609
610
.class auto ansi nested public NestedPublicClass
extends [mscorlib]System.Object
624
625

The nested classes all line up as well with NestedProtectedFriendClass being defined the same way as NestedProtectedInternalClass

.class interface public abstract auto ansi VbClass.IInterface
651

The Interface definitions match.

.class public abstract auto ansi VbClass.MustInheritClass
extends [mscorlib]System.Object
655
656
.class public auto ansi sealed VbClass.NotInheritableClass
extends VbClass.MustInheritClass
670
671

As expected MustInheritClass and NotInheritableClass align with AbstractClass and SealedClass respectively

.class public auto ansi VbClass.DerivedClass
extends VbClass.MustInheritClass
691
692
.class public auto ansi VbClass.InterfaceClass
extends [mscorlib]System.Object
implements VbClass.IInterface
706
707
708

The derived and interface implementation classes also line up with their C# counterparts. Interestingly both IL and VB use the Implements keyword to indicate a class is implementing an interface but VB uses Inherits to indicate a derived class while IL uses extends.

.class public sequential ansi sealed VbClass.PublicStructure
extends [mscorlib]System.ValueType
722
723

The Structure definition matches the C# struct

.class public auto ansi sealed VbClass.PublicEnum
extends [mscorlib]System.Enum
729
730

The enums also line up although interestingly VB won't let you create an enum with no values defined

The only class defined in C# that we don’t have in VB is StaticClass. The VB version of static is Shared but you can’t use shared on a class. Instead VB has the concept of a Module.

.class public auto ansi sealed VbClass.PublicModule
extends [mscorlib]System.Object
685
686

Modules work much the same way as C# static classes except that they aren’t called classes and that they are automatically visible throughout the code-base. As you can see this is just a VB concept as modules also get compiled into IL classes. It’s interesting to note that while C# compiles static classes as abstract and sealed VB only compiles modules as sealed. To prevent modules being instantiated VB compiles them without a constructor.

As you can see the distinctions made by higher level languages are removed in exchange for the simplicity of IL. Everything is a class in the end.

Next time we’ll look at field definitions and start digging into how classes work.

Comments: