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 ClassNote You can use the Visual Studio .NET code editor to add a class to your startup form by typing the Class keyword followed by the name of the new class. The code editor provides a corresponding End Class statement for you.
-
To simplify access to the registry classes, add an Imports statement to the top of the source code containing the class statement:
Imports Microsoft.Win32
-
Define three private fields for the class by placing the following code between the Class and End Class statements:
Private pVal As String ' Holds the LastFile property value.
Private KeyName As String ' Holds the name of the registry key.
Private SubKeyName As String ' Holds the name of the subkey.These three fields are declared as Private, and can be used only from within the class. You can make fields accessible from outside a class by using access modifiers that provide greater access, such as Public.
-
Define a property for the class by adding the following code:
Public Property LastFile() As String
Get ' Retrieves the property value.
Return pVal
End Get
Set(ByVal Value As String)
pVal = Value
End Set
End Property
-
Define methods for the class by adding the following code:
Sub SaveStateInfo()
' Save the current value of the LastFile property to the registry.
Dim aKey As RegistryKey
' Create a key if it does not exist.
aKey = Registry.CurrentUser.CreateSubKey(KeyName)
' Save the property value to the registry.
aKey.SetValue(SubKeyName, pVal)
End Sub
Sub GetStateInfo()
' Restore the property value LastFile from the
' value stored in the registry.
Dim aKey As Object
Dim myRegKey As RegistryKey = Registry.CurrentUser
Try
' This call goes to the Catch block if the registry key is not set.
myRegKey = myRegKey.OpenSubKey(KeyName)
Dim oValue As Object = myRegKey.GetValue(SubKeyName)
pVal = CStr(oValue)
Catch
pVal = "" ' Set to default if key value cannot be read.
End Try
End Sub
-
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 SubThe 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
LastFileproperty 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.
-
Define a Finalize destructor for the class by adding a procedure called
Finalize:
Protected Overrides Sub Finalize()
' Save the value of the LastFile property to the registry
' when the class loses scope.
SaveStateInfo()
MyBase.Finalize() ' Call Finalize on the base class.
End SubThe Finalize destructor saves the value stored in the property to the registry after the class loses scope.

Join the conversation! Your thoughts help the community grow.