- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Organizing code in Visual Basic
In this part of the Visual Basic tutorial, we will show, how to organise code. We will cover modules, procedures and namespaces and the scope.
Visual Basic statements are organised into blocks, modules, classes and namespaces. This helps to make the code more maintainable and robust. Correct code organisation prevents from making errors in the code.
The basic building blocks of a Visual Basic program are:
- Assembly
- Namespace
- Modules
- Classes
- Procedures and functions
- Blocks
- Statements
An assembly is a DLL or exe file. An assembly is a compiled code library for use in deployment, versioning and security. A namespace is an abstract container providing context for the items. A module is a reference type available throughout its namespace. Classes are basic building blocks of an OOP program. A procedure is a unit of a program that is created to do a specific task. A block is the lowest level organisation of Visual Basic statements provided by some keywords like If
or While
. A statement is an atom in a Visual Basic program, a smallest unit of code.
Closely related to this topic is the scope and duration of a variable. A scope is the visibility of the declared variable.
Scope | Description |
---|---|
Block scope | Available only within the code block in which it is declared |
Procedure scope | Available within the procedure in which it is declared |
Module scope | Available to all code within the module, class, or structure in which it is declared |
Namespace scope | Available to all code in the namespace in which it is declared |
A lifetime of a variable is a period of time during which a variable holds a value. Local variables exists as long as the procedure is executing. After that, they are not available anymore. However, if we declare a variable to be Static, the variable continues to exist after the procedure terminates. Module, Shared and instance variables exist for the lifetime of the application.
The basic example
First, we cover some basics.
Option Strict On Module Example Sub Main() Console.WriteLine("Simple example") End Sub End Module
In this example, we have a Module called Example
. Inside the example, we have a Main()
subroutine. The statement that prints some message to the console is placed within the Main()
procedure. Event the most simple Visual Basic programs must be properly organised.
Option Strict On Public Class Example Public Shared Sub Main() Console.WriteLine("Simple example") End Sub End Class
The exact example, now without the module. We can put the code inside a class too. The Main(
procedure must be declared Shared
, because the class is not instantiated. The compiler calls the Main()
method without creating an instance of the class. That is why it must be declared Shared
. Java and C# work the same.
Namespaces
Namespaces are used to organise code at the highest logical level. They classify and present programming elements that are exposed to other programs and applications. Within a namespace, we can declare another namespace, a class, an interface, a structure, an enumeration, or a delegate.
In the following code, we have two files that share the same namespace.
Option Strict On NameSpace ZetCode Module Example1 Public Dim x As Integer = 0 Sub Init() x += 100 Console.WriteLine(x) End Sub End Module End NameSpace
We have a ZetCode
namespace. In the namespace, we have a module Example1
.
NameSpace ZetCode
We declare a namespace called ZetCode
.
Public Dim x As Integer = 0
In the module, we declare and initialise a x
variable.
Sub Init() x += 100 Console.WriteLine(x) End Sub
We have an Init()
method, in which we work with the x
variable.
Option Strict On NameSpace ZetCode Module Example Sub Main() Init() x += 100 Console.WriteLine(x) End Sub End Module End NameSpace
In the second file, we work with the Init()
method from the previous file.
NameSpace ZetCode
We work in the same namespace.
Init() x += 100 Console.WriteLine(x)
We call the Init()
procedure and work with the x
variable. Both the procedure and the x
variable are defined in a different file and different module. But they are defined in the same namespace, so we can use them.
$ ./samenamespace.exe 100 200
Output.
The following code example has two distinct namespaces. We use the Imports
keyword to import elements from a different namespace.
Option Strict On NameSpace MyMath Public Class Basic Public Shared PI As Double = 3.141592653589 Public Shared Function GetPi() As Double Return Me.PI End Function End Class End NameSpace
We have a skeleton of a Math
class in a MyMath
namespace. In the Basic class, we define a PI
constant and a GetPi()
method.
Option Strict On Imports MyMath NameSpace ZetCode Public Class Example Public Shared Sub Main() Console.WriteLine(Basic.PI) Console.WriteLine(Basic.GetPi()) End Sub End Class End NameSpace
In this file, we use the elements from the MyMath
namespace.
Imports MyMath
We import the elements from the MyMath
namespace into our namespace.

On Visual Basic 2008 Express edition, there is a root namespace automatically created. This can be found under project properties, Application tab. Either delete the root namespace or include it into the imports path. For example, if you have Testing there, change the line to Imports Testing.MyMath
.
Console.WriteLine(Basic.PI) Console.WriteLine(Basic.GetPi())
Now we can use those elements. In our case the PI
variable and the GetPi()
method.
Option Strict On ' Imports MyMath NameSpace ZetCode Public Class Example Public Shared Sub Main() Console.WriteLine(MyMath.Basic.PI) Console.WriteLine(MyMath.Basic.GetPi()) End Sub End Class End NameSpace
Note that we do not need the Imports
keyword. In the example, it is commented out. We can use elements from other namespaces by using fully qualified names of the elements.
Modules
A module is used to organise code and wrap up variables, properties, events, and procedures of similar use. Unlike a class, a module is not a type. A module can be created in a namespace or in a file. A module cannot be created inside another module, class, structure, interface or block. All members in a module are implicitly Shared. Modules have a Friend access. This means that a module is accessible everywhere in an assembly.
Option Strict On Module First Public x As Byte = 11 Public Sub FirstModule() Console.WriteLine("First module") End Sub End Module Module Second Public y As Byte = 22 Public Sub SecondModule() Console.WriteLine("Second module") End Sub End Module Module Example Sub Main() Console.WriteLine(x) Console.WriteLine(Second.y) FirstModule() SecondModule() End Sub End Module
We have three modules defined. The first two modules have variables and procedures. These will be used in the third module.
Module First Public x As Byte = 11 ... End Module
We can use access specifiers inside modules too. This way we can control the accessibility of the elements in the modules.
Console.WriteLine(x) Console.WriteLine(Second.y)
We print the x
and y
variables. They are Public
and are accessible from a different module. We may use the module name to fully specify the variable name.
A scope is a visibility of a variable. A variable with a module scope is available within the module, where it was declared.
Option Strict On Module Example Private x As Integer = 0 Sub Main() proc1() proc2() proc3() End Sub Sub proc1() Console.WriteLine(x) End Sub Sub proc2() x += 100 Console.WriteLine(x) End Sub Sub proc3() x += 100 Console.WriteLine(x) End Sub End Module
We have x
variable inside the module. The variable is available in all three procedures.
Private x As Integer = 0
This is a variable with a module scope. It is declared outside any procedure.
Sub proc2() x += 100 Console.WriteLine(x) End Sub
Inside the proc2()
procedure, we increase the x
variable and print its contents to the console. We refer to the x
variable defined in the module.
$ ./modulescope.exe 0 100 200
Output of the example.
Procedures
Procedures provide modularity to the code project. They should do only a specific task.
Option Strict On Module Example Dim x As Integer = 0 Sub Main() Console.WriteLine(x) proc1() proc2() proc3() End Sub Sub proc1() Dim x As Integer x += 100 Console.WriteLine(x) End Sub Sub proc2() Dim x As Integer x += 100 Console.WriteLine(x) End Sub Sub proc3() Dim x As Integer x += 100 Console.WriteLine(x) End Sub End Module
In the preceding code example, we have three procedures beside the main procedure. The three procedures create a local x
variable and print it to the terminal. The main procedure refers to the module x
variable.
Sub proc1() Dim x As Integer x += 100 Console.WriteLine(x) End Sub
The proc1()
procedure creates a local x
variable. This variable shadows the one, declared at a module scope.
$ ./procedurescope.exe 0 100 100 100
The main procedure prints 0. The other procedures print 100 to the terminal. They create their local x
variables, initiate them to 0, increase by 100.
Block scope
It is important to understand that variables declared within a block of code like If
/ End If
or While
/ End While
have a limited block scope and lifetime. The next example illustrates this.
Option Strict On Module Example Sub Main() If True Console.WriteLine("Inside If block") Dim x As Integer = 0 Console.WriteLine(x) x += 500 Console.WriteLine(x) End If Console.WriteLine("Outside If block") Rem Will not compile Rem Console.WriteLine(x) End Sub End Module
We have an x
variable declared Inside the If
/ End If
block.
Rem Will not compile Rem Console.WriteLine(x)
The variable is not available outside the block. If we uncomment the second line, the example will not compile.
This part of the Visual Basic tutorial was dedicated to organising code. We mentioned basic organisational elements of the code like namespaces, modules or procedures. We described variable scope and duration, which is closely related to those elements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论