This walkthrough demonstrates how to use class modules to define classes, from which you can then create objects. It also shows you how to create properties and methods for the new class, and demonstrates how objects are initialized.
To define a class
-
Open a new Windows Application project by clicking New on the File menu, and then clicking Project. The New Project dialog box appears.
-
Select Windows Application from the list of Visual Basic project templates. The new project is displayed.
-
Add a new class to the project by clicking Add Class on the Project menu. The Add New Item dialog box appears.
-
Name the new module StateInfo.vb, and then click Open. The code for the new class is displayed:
Public Class StateInfo
End Class
Define a parameterized constructor for the new class by adding a procedure named Sub New:
Sub New(ByVal RegistryKeyName As String, _
ByVal RegistrySubKeyName As String)
' Save the names of the registry key and subkey in two fields.
KeyName = RegistryKeyName
SubKeyName = RegistrySubKeyName
' Restore the value of the LastFile property from the registry.
MyClass.GetStateInfo()
End Sub
The Sub New constructor is called automatically when an object based on this class is created. This constructor sets the value of the fields that hold the registry key and subkey names, and calls a procedure that restores the value of the LastFile property from the registry.
Note This walkthrough uses the registry to store state information for the class. Information stored in the registry is available to other applications and users, and therefore it should not be used to store security, or critical application information.