CLASSES, STRUCTURES, AND MODULES

Classes, structures, and modules create their own name contexts that are similar in some ways to namespaces. For example, a class or structure can contain the definition of another class or structure, as shown in the following code:

Public Class Class1
    Public Class Class2
        ...
    End Class
 
    Public Structure Struct1
        Public Name As String
 
        Public Structure Struct2
            Public Name As String
        End Structure
    End Structure
End Class

You can access public module members and shared class or structure members using a fully qualified syntax similar to the one used by namespaces. For example, the following code creates the GlobalValues module and defines the public variable MaxJobs within it. Later, the program can set MaxJobs using its fully qualified name.

Module GlobalValues
    Public MaxJobs As Integer
    ...
End Module
...
MyApplication.GlobalValues.MaxJobs = 100

Although these cases look very similar to namespaces, they really are not. One big difference is that you cannot use a Namespace statement inside a class, structure, or module.

IntelliSense gives another clue that Visual Basic treats classes, structures, and modules differently from namespaces. The IntelliSense pop-up shown in Figure 24-2 displays curly braces { } next to the FinanceStuff and JobClasses namespaces, but it displays different icons for the classes Employer and Form1, and the module Module1. When you select a namespace, IntelliSense also displays a tooltip (on the right in Figure 24-2) indicating ...

Get Visual Basic 2012 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.