Modules, classes and structures all look very similar in VB.NET, so it wouldn't be suprising to hear that they are often confused. This article will look at all of them and the difference between them.
Program structure
Modules, classes and structures are all part of program structure - they are used to seperate the program into more manageable chunks of code. They all serve individual purposes but can contain almost the same kind of data.
Class
A class is a way of storing variables (data) and methods. All code goes into a class of some kind. A class can have an access modifier applied to it so that it can be hidden or publicly exposed.
Within a class exist variables. They can be declared Public
, Private
, Protected
or Friend
. This
limits the scope of the variable.The access modifier of the
class defines the overall access modifier for all the variables
and methods.
If a variable is declared Public
in a Private
class, it cannot be used elsewhere.
Methods also exist within a class. They too can be given an
access modifier. They can be called by using the Call
statement or
by simply typing their path such as: Class.Method()
.
A class can be instantiated, meaning that it can be created as a new copy of the class (an instance of a class). Classes tend to work this way.
Public Class SampleClass 'Code goes here End Class
Module
A module is a very simple 'version' of a class.
Modules are static, meaning that they do not change (hence why
in the C family of languages the equivalent of a VB.NET module
is a public static
method). In this case, they are
not instantiated. This is why Console Applications use modules.
Modules can be seen as helper classes, meaning that the methods
within it are used to support the main program. Because of the
fact that modules are static, they may not be inherited.
Below is a sample of how a Console Application looks, note
the fact that it is a module with an entry point (a place for
the program to start running at) called Sub New()
.
Module Module1 Sub Main() 'Code goes here End Sub End Module
Structures
Structures are stored in memory differently to classes but on the top they work similarly to classes. Their uses are limited but can be used to store smaller pieces of data, for instance a 3D point. The following is such an example:
Public Structure SampleStructure Public x As Integer Public y As Integer Public z As Integer Public Sub SampleMethod() 'Code goes here End Sub End Structure
Structures can be used for storing records similar to the way they are done in databases.
There is more on classes and structures here.
-
Write a program that uses structures that store information about a person. You may
want to call this structure
student
. -
Within the
student
, declare 3 fields:first_name
,last_name
andtest_score
. -
Again within the structure, declare a method (subroutine) that will print the student's first name,
last name and their test score to the display called
display
. -
Produce an array of three
student
instances with different names and test scores in each of them. -
Create a
For Each
loop that iterates the list and runs thedisplay
method for each student.