From the course: Visual Basic Essential Training
Understand code containers: Modules and classes - Visual Basic Tutorial
From the course: Visual Basic Essential Training
Understand code containers: Modules and classes
- [Instructor] All Visual Basic code must reside within a container. The primary containers are modules, classes, and structures. All code must reside within a container for the application to compile. However, the main purpose of these containers is not just to meet this requirement. They help us organize code into logical, manageable sections, improving clarity, maintainability, and reusability. The three main types serve different purposes. Understanding their roles is key to writing efficient and maintainable code. Modules are static containers that provide globally accessible methods and constants. They are perfect for utility functions or global variables that multiple parts of your program need to access. Typically, Sub Main is placed inside a module because modules are static by nature and do not require instantiation, making them ideal for entry points. Having one big Sub Main method is not ideal, while for simple applications, a monolithic approach might suffice. In real world scenarios, it's important to subdivide your code into other containers for better organization and maintainability. By using classes, modules, and structures, you can create logical divisions within your application that make it easier to understand, easier to test, and easier to extend. Classes are the backbone of object-oriented programming in Visual Basic. They allow us to encapsulate data and behavior together, providing a blueprint for creating objects that represent real world entities. Unlike modules, classes support instant specific data, and they also support features like inheritance and polymorphism, making them ideal for modeling complex systems or reusable components. Most of the types available on .NET are built from classes. There are thousands of them available for us to use in our code. Most of the code we write in Visual Basic will be in classes. When we look at the WPF or the ASP.NET template code, in another video, we'll see that classes are the default for everything. Structures are designed for small, simple data objects. In .NET, they are known as value types. What that means for us is that a structure is a type that is memory-efficient and ideal for scenarios where the data they represent is small, or it's immutable, or it's used temporarily. This could be like a point structure to represent coordinates, or a color structure to store RGB values. Unlike classes, structures do not support inheritance and are typically used to store plain data rather than behavior. Here's how we can define each of these containers. Let's start by looking at line three. I'm declaring the start of a module. Now, I'm using a Public Visual Basic word that's a Public keyword that represents the scope of this module. We'll talk more about that later. The key part for this is the word module. That's the VB keyword that says this is where the module starts, and then next is the name of the module. Then within that, I can define my members. Line seven is where I end the module. In Visual Basic, that line of code has the End keyword at the beginning, End Module. It's similar for the class and the structure. You start with the word Class and then the name of the class customer; you start with the word Structure and the name of the structure, and then you end by saying End Class or End Structure.