加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

VB.NET 学习--(3)

发布时间:2020-12-16 23:33:51 所属栏目:大数据 来源:网络整理
导读:sual Basic 语言概念演练:定义类 转载:http://www.cbcz.com/study/doc/200704142921.htm 此演练说明如何使用类模块定义类,然后从中创建对象。同时还说明如何为新类创建属性和方法,以及如何初始化对象。 定义类 通过在“文件”菜单上单击“新建”,然后单

sual Basic 语言概念演练:定义类

转载:http://www.cbcz.com/study/doc/200704142921.htm

此演练说明如何使用类模块定义类,然后从中创建对象。同时还说明如何为新类创建属性和方法,以及如何初始化对象。

定义类

  1. 通过在“文件”菜单上单击“新建”,然后单击“项目”,打开一个新的“Windows 应用程序”项目。将出现“新建项目”对话框。
  2. 从 Visual Basic 项目模板的列表中选择“Windows 应用程序”。将显示新项目。
  3. 在“项目”菜单中单击“添加类”,将一个新类添加到项目中。将出现“添加新项”对话框。
  4. 将新模块命名为StateInfo.vb,然后单击“打开”。即会显示新类的代码:
    Public Class StateInfo
    End Class
    注意可以使用 Visual Studio .NET 代码编辑器,在新类的名称之后键入 Class 关键字,将新类添加到启动窗体中。代码编辑器会提供相应的 End Class语句。
  5. 若要简化对注册表类的访问,请在包含类语句的源代码顶部添加一条Imports语句:
    Imports Microsoft.Win32
  6. ClassEnd Class语句之间加入以下代码,为类定义三个私有字段:
       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.

    这三个字段被声明为Private,只能在类的内部使用。可以通过使用提供更大访问权限的访问修饰符(如Public)来使字段得以从类的外部进行访问。

  7. 通过添加以下代码为类定义属性:
    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
  8. 通过添加以下代码为类定义方法:
    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)
    'CreateSubKey(subKey As String)打开一个现有的子项,或者新建一个新子项进行访问,
    ‘ Registry 提供表示windows注册表中根项的Microsoft.Win32.RegisterKey对象
    '  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
  9. 通过添加名为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

    当创建基于此类的对象时,会自动调用Sub New构造函数。此构造函数设置包含注册表项和子项名称的字段的值,并调用一个从注册表中恢复LastFile属性值的过程。

    注意此演练使用注册表来存储类的状态信息。其他应用程序和用户也可以使用存储在注册表中的信息,因此,不应使用注册表来存储安全性信息或重要的应用程序信息。
  10. 通过添加名为Finalize的过程为类定义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 Sub

    在类失去范围后,析构函数Finalize将存储在属性中的值保存到注册表。

创建测试类的按钮

  1. 右击解决方案资源管理器中启动窗体的名称,将启动窗体更改为设计模式,然后单击“视图设计器”。默认情况下,“Windows 应用程序”项目的启动窗体的名称为 Form1.vb。将显示主窗体。
  2. 在主窗体中添加一个按钮,然后双击该按钮显示Button1_Click事件处理程序的代码。添加下列代码以调用测试过程:
    '  Create an instance of the class and pass the registry key
    '  names to the Sub New constructor.
       Dim SI As New StateInfo("SoftwareStateInfo","PropertyValue")
    '  Display the value of the property if it has been restored.
       If Len(SI.LastFile) > 1 Then
          MsgBox("The value of the property LastFile is: " _
                 & SI.LastFile)
       Else
          MsgBox("The LastFile property has not been set.")
       End If
       SI.LastFile = "C:BinaryFile.txt" ' Set the property.
    '  Ask the object to save the property value to the registry.
       SI.SaveStateInfo()

运行应用程序

  1. 按 F5 运行应用程序。将显示应用程序。
  2. 单击窗体上的按钮以调用测试过程。第一次进行此操作时,将显示一条消息,说明尚未设置LastFile属性。
  3. 单击“确定”消除该消息框。Button1_Click过程设置LastFile属性值,并调用SaveState方法。即使没有在Button1_Click中显式调用SaveState,也会在关闭启动窗体后通过Finalize方法自动调用它。
  4. 再一次单击该按钮。将显示消息“属性 LastFile 的值位于 C:BinaryFile.txt”。
  5. 关闭主窗体,然后按 F5 键重新启动程序。创建了一个基于该类的对象,其Sub New构造函数调用GetStateInfo过程,该过程从注册表中恢复属性值。单击该按钮后,将再次显示“The value of the property LastFile is C:BinaryFile.txt”消息。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读