Classes
Classes are the 'right' place to start because, as Microsoft notes, "A class is a fundamental building block of object-oriented programming (OOP)." In fact, some authors treat modules and structures as just special kinds of classes. A class is more object oriented than a module because it's possible to instantiate (make a copy of) a class but not a module.
In other words, you can code ...
Public Class Form1
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim myNewClass As Class1 = New Class1
myNewClass.ClassSub()
End Sub
End Class
(The class instantiation is emphasized.)
It doesn't matter whether the actual class itself, in this case, ...
Public Class Class1
Sub ClassSub()
MsgBox("This is a class")
End Sub
End Class
... is in a file by itself or is part of the same file with the Form1 code. The program runs exactly the same way. (Notice that Form1 is a class too.)
You can also write class code that behaves much like a module, that is, without instantiating it. This is called a Shared class. The article "Static" (that is, "Shared") versus Dynamic Types in VB.NET explains this in much more detail.
Another fact about classes should also be kept in mind. Members (properties and methods) of the class only exist while the instance of the class exists. The name for this is scoping. That is, the scope of an instance of a class is limited. The code above can be changed to illustrate this point this way:
Public Class Form1
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim myNewClass As Class1 = New Class1
myNewClass.ClassSub()
myNewClass = Nothing
myNewClass.ClassSub()
End Sub
End Class
When the second myNewClass.ClassSub() statement is executed, a NullReferenceException error is thrown because the ClassSub member doesn't exist.
Modules
In VB 6, it was common to see programs where most of the code was in a module (A .BAS, file rather than, for instance, in a Form file such as Form1.frm.) In VB.NET, both modules and classes are in .VB files. The main reason modules are included in VB.NET is to give programmers a way to organize their systems by putting code in different places to fine tune the scope and access for their code. (That is, how long members of the module exist and what other code can reference and use the members.) Sometimes, you may want to put code into separate modules just to make it easier to work with.
All VB.NET modules are Shared because they can't be instantiated (see above) and they can be marked Friend or Public so they can be accessed either within the same assembly or whenever they're referenced.
Are Structures another type of object? Find out on the next page.

