VB2010(14)_结构集合散列表

谁说胖子不能爱 提交于 2020-01-18 22:13:29

Public Class Form1
    '初始化一个实例
    Private Sub InitiCustomer()
        Dim objCustomer As Customer
        objCustomer.FirstName = "Michael"
        objCustomer.LastName = "Dell"
        objCustomer.Email = "mdell@somecompany.com"

        DisplayCustomer(objCustomer)
    End Sub
    '显示一个实例
    Private Sub DisplayCustomer(ByVal customer As Customer)
        txtName.Text = customer.name
        txtFirstName.Text = customer.FirstName
        txtLastName.Text = customer.LastName
        txtEmail.Text = customer.Email
    End Sub
    '创建一个Customer结构对象实例
    Public Sub CreaterCustomer( _
               ByVal FirstName As String, _
               ByVal LastName As String, _
               ByVal Email As String _
               )

        Dim objNewCustomer As Customer

        objNewCustomer.FirstName = FirstName
        objNewCustomer.LastName = LastName
        objNewCustomer.Email = Email

        '列表或集合中添加一个新对象
        objCustomers.Add(objNewCustomer)

        lstCustomers.Items.Add(objNewCustomer)
    End Sub
    '被选元素的属性
    Public ReadOnly Property SelectCustomer() As Customer
        Get
            If lstCustomers.SelectedIndex <> -1 Then
                Return objCustomers(lstCustomers.SelectedIndex)
            End If
        End Get
    End Property
    '创建一个列表
    'Private objCustomers As New ArrayList

    '创建一个集合
    Private objCustomers As New CustomerCollection
    '列表按钮
    Private Sub btnListCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnListCustomer.Click
        objCustomers.clear()
        lstCustomers.Items.Clear()
        '参数表为firstname,lastname,Email
        CreaterCustomer("a1", "a2", "a@163.com")
        CreaterCustomer("b1", "b2", "b@163.com")
        CreaterCustomer("c1", "c2", "c@163.com")
    End Sub
    '删除按钮
    Private Sub BtnDeleteCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDeleteCustomer.Click
        If lstCustomers.SelectedIndex = -1 Then
            MessageBox.Show("You must Selet a Customer to Delete.", "Structure Demo")
            Exit Sub
        End If

        If MessageBox.Show("Are you sure?", "Structure Demo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
            Dim objCustomerToDelete As Customer = SelectCustomer
            objCustomers.Remove(objCustomerToDelete)
            lstCustomers.Items.Remove(objCustomerToDelete)
        End If
    End Sub
    Private Sub lstCustomers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstCustomers.SelectedIndexChanged
        DisplayCustomer(SelectCustomer)
    End Sub
    Private Sub btnLookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLookup.Click
        Dim objFoundcustomer As Customer = objCustomers(txtEmail.Text)
        If Not IsNothing(objFoundcustomer.Email) Then
            MessageBox.Show("The customer name is:" & objFoundcustomer.name, "Structuse Demo")
        Else
            MessageBox.Show("There is no customer with the email" & "address" & txtEmail.Text & ".", "structure Demo")
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitiCustomer()
    End Sub
End Class
-------------------------------

 Customer结构

Public Structure Customer
    Public FirstName As String
    Public LastName As String
    Public Email As String
    Public ReadOnly Property name() As String
        Get
            Return FirstName & "·" & LastName
        End Get
    End Property
    '结构的ToString方法默认返回结构名,所以要重写ToString方法
    Public Overrides Function ToString() As String
        Return name & ", Email:" & Email
    End Function
End Structure
——————————————————————————————————————

Customer集合

Public Class CustomerCollection
    Inherits CollectionBase

    Private objEmailHashtable As New Hashtable

    Public ReadOnly Property EmailHashtable() As Hashtable
        Get
            Return objEmailHashtable
        End Get
    End Property
    Default Public Property item(ByVal index As Integer) As Customer
        Get
            Return CType(Me.List.Item(index), Customer)
        End Get
        Set(ByVal value As Customer)
            Me.List.Item(index) = value
        End Set
    End Property
    Default Public ReadOnly Property item(ByVal email As String) As Customer
        Get
            Return CType(EmailHashtable.Item(email.ToLower), Customer)
        End Get
    End Property
    '添加
    Public Sub Add(ByVal newCustomer As Customer)
        Me.List.Add(newCustomer)
        EmailHashtable.Add(newCustomer.Email.ToLower, newCustomer)
    End Sub
    '删除
    Public Sub Remove(ByVal oldcustomer As Customer)
        Me.List.Remove(oldcustomer)
        EmailHashtable.Remove(oldcustomer.Email.ToLower)
    End Sub
    Public Shadows Sub removeAt(ByVal index As Integer)
        Remove(item(index))
    End Sub

    Public Shadows Sub clear()
        MyBase.OnClear()
        EmailHashtable.Clear()
    End Sub
End Class
 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!