2022-10-29 - In IL: VB Classes, Modules and Structures
In IL
- Part 1 - Introduction
- Part 2 - Variables and Types
- Part 3 - Variables in Visual Basic .NET
- Part 4 - Instructions and the Stack
- Part 5 - Volume of a Cylinder (Operations)
- Part 6 - Branching Instructions
- Part 7 - Largest of Two Numbers (if-else)
- Part 8 - Largest of Three Numbers (If-ElseIf-Else)
- Part 9 - Switch instruction
- Part 10 - Grade Analyser (switch)
- Part 11 - Prize Calculator (switch-2)
- Part 12 - VB Grade Analyser (Select)
- Part 13 - Loop Instructions
- Part 14 - Print the Alphabet (while)
- Part 15 - Print the Alphabet (do while, for)
- Part 16 - Print the Alphabet (Do Until)
- Part 17 - Print the Alphabet (break, continue)
- Part 18 - Array Instructions
- Part 19 - Summing Arrays
- Part 20 - Other Instructions
- Part 21 - Assemblies
- Part 22 - Class Definitions
- Part 23 - C# Classes and Structs
- Part 24 - VB Classes, Modules and Structures
- Part 25 - Field Definitions
- Part 26 - Field Declarations
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.
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.
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.
The nested classes all line up as well with NestedProtectedFriendClass being defined the same way as NestedProtectedInternalClass
The Interface definitions match.
As expected MustInheritClass and NotInheritableClass align with AbstractClass and SealedClass respectively
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.
The Structure definition matches the C# struct
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.
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: