Braganca@fe.up.pt

System.Collections

System.Collections Namespace

Creating Your Own Collection Class

IDisposable Interface

 

 

Public Class Form1

 

    Dim GrupoPessoas As New populacao

 

    Dim NPessoas As Integer = 0

 

    Private Sub Pessoa2ListBox(ByVal chave As String)

        Me.ListBoxCod.Items.Add(GrupoPessoas.item(chave).Codigo)

        Me.ListBoxNome.Items.Add(GrupoPessoas.item(chave).Nome)

        Me.ListBoxDataN.Items.Add(GrupoPessoas.item(chave).DataNascimento)

        Me.ListBoxIdade.Items.Add(GrupoPessoas.item(chave).Idade)

        TextBoxDictionaryCount.Text = GrupoPessoas.count

    End Sub

 

   

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim pessoa As New Pessoa

        pessoa = New Pessoa(Me.TextBox4.Text, Me.TextBox5.Text, Me.TextBox6.Text)

        GrupoPessoas.Add(pessoa)

        Pessoa2ListBox(Me.TextBox4.Text)

    End Sub

 

 

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLimpar.Click

        Me.ListBoxCod.Items.Clear()

        Me.ListBoxNome.Items.Clear()

        Me.ListBoxDataN.Items.Clear()

        Me.ListBoxIdade.Items.Clear()

    End Sub

 

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        Pessoa2ListBox(Me.TextBox7.Text)

    End Sub

 

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        For Each estranho As Pessoa In GrupoPessoas

            Pessoa2ListBox(estranho.Codigo)

        Next

    End Sub

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Pessoa2ListBox(GrupoPessoas.itemByNome(TextBox1.Text).Codigo)

 

    End Sub

End Class

 


 

Public Class Pessoa

 

    Implements IDisposable

 

    Private L_Codigo As String = ""

    Private L_Nome As String = ""

    Private L_DataNasc As Date = Nothing

 

    Public Sub New()

 

    End Sub

 

    Public Sub New(ByVal Codigo As String)

        L_Codigo = Codigo

    End Sub

 

    Public Sub New(ByVal Codigo As String, ByVal nome As String)

        L_Codigo = Codigo

        L_Nome = nome

    End Sub

 

    Public Sub New(ByVal Codigo As String, ByVal nome As String, ByVal DataNasc As Date)

        L_Codigo = Codigo

        L_Nome = nome

        L_DataNasc = DataNasc

    End Sub

 

    Public Property Codigo() As String

        Get

            Return L_Codigo

        End Get

        Set(ByVal value As String)

            L_Codigo = value

        End Set

    End Property

 

    Public Property Nome() As String

        Get

            Return L_Nome

        End Get

        Set(ByVal value As String)

            L_Nome = value

        End Set

    End Property

 

    Public Property DataNascimento() As Date

        Get

            Return L_DataNasc

        End Get

        Set(ByVal value As Date)

            L_DataNasc = value

        End Set

    End Property

 

    Public ReadOnly Property Idade() As Integer

        Get

            Return DateDiff(DateInterval.Year, L_DataNasc, Today)

        End Get

 

    End Property

 

    Private disposedValue As Boolean = False        ' To detect redundant calls

 

    ' IDisposable

    Protected Overridable Sub Dispose(ByVal disposing As Boolean)

        If Not Me.disposedValue Then

            If disposing Then

                ' TODO: free other state (managed objects).

            End If

 

            ' TODO: free your own state (unmanaged objects).

            ' TODO: set large fields to null.

        End If

        Me.disposedValue = True

    End Sub

 

#Region " IDisposable Support "

    ' This code added by Visual Basic to correctly implement the disposable pattern.

    Public Sub Dispose() Implements IDisposable.Dispose

        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.

        Dispose(True)

        GC.SuppressFinalize(Me)

    End Sub

#End Region

 

End Class

 

 


Public Class populacao

    Inherits Collections.CollectionBase

 

    Private ObjCodigoHashtable As New Hashtable

    Private ObjNomeHashtable As New Hashtable

 

    Private _count As Integer

 

 

    Public Sub Add(ByVal NewPessoa As Pessoa)

        Me.List.Add(NewPessoa)

 

        ObjCodigoHashtable.Add(NewPessoa.Codigo.ToLower, NewPessoa)

        ObjNomeHashtable.Add(NewPessoa.Nome.ToLower, NewPessoa)

 

        _count = Me.List.Count

    End Sub

 

    Public Sub Remove(ByVal oldPessoa As Pessoa)

        Me.List.Remove(oldPessoa)

 

        ObjCodigoHashtable.Remove(oldPessoa.Codigo.ToLower)

        ObjNomeHashtable.Remove(oldPessoa.Nome.ToLower)

    End Sub

 

    Default Public Property item(ByVal index As Integer) As Pessoa

        Get

            Return Me.List.Item(index)

        End Get

        Set(ByVal value As Pessoa)

            Me.List.Item(index) = value

        End Set

    End Property

 

    Default Public ReadOnly Property item(ByVal Codigo As String) As Pessoa

        Get

            Return ObjCodigoHashtable.Item(Codigo.ToLower)

        End Get

 

    End Property

    Public ReadOnly Property itemByNome(ByVal Nome As String) As Pessoa

        Get

            Return ObjNomeHashtable.Item(Nome.ToLower)

        End Get

 

    End Property

    Public ReadOnly Property ExistsNome(ByVal Nome As String) As Boolean

        Get

            If IsNothing(ObjNomeHashtable.Item(Nome.ToLower)) Then

 

                Return False

            Else

                Return True

            End If

        End Get

 

    End Property

 

  

    Private ReadOnly Property CodigoHashtable() As Hashtable

        Get

            Return ObjCodigoHashtable

        End Get

 

    End Property

    Private ReadOnly Property NomeHashtable() As Hashtable

        Get

            Return ObjNomeHashtable

        End Get

 

    End Property

 

    Public Shadows Sub clear()

        MyBase.Clear()

        CodigoHashtable.Clear()

        NomeHashtable.Clear()

 

    End Sub

 

    Public Shadows Sub RemoveAt(ByVal index As Integer)

        Remove(item(index))

 

    End Sub

End Class