System.Collections
Creating Your Own Collection Class
Dim GrupoPessoas
As
New populacao
Dim NPessoas
As
Integer = 0
Private
Sub Pessoa2ListBox(ByVal
index As
String)
Me.ListBoxCod.Items.Add(GrupoPessoas.item(index).Codigo)
Me.ListBoxNome.Items.Add(GrupoPessoas.item(index).Nome)
Me.ListBoxDataN.Items.Add(GrupoPessoas.item(index).DataNascimento)
Me.ListBoxIdade.Items.Add(GrupoPessoas.item(index).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(GrupoPessoas.Count - 1)
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
Dim I
As
Integer = 0
For
Each estranho
As Pessoa
In GrupoPessoas
Pessoa2ListBox(i)
i = i + 1
Next
End
Sub
End
Class
Public
Class Pessoa
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
End
Class
Public
Class populacao
Inherits
Collections.CollectionBase
Private _count
As
Integer
Public
Sub Add(ByVal
NewPessoa As Pessoa)
Me.List.Add(NewPessoa)
_count = Me.List.Count
End
Sub
Public
Sub Remove(ByVal
oldPessoa As Pessoa)
Me.List.Remove(oldPessoa)
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
Public
Shadows
Sub clear()
MyBase.Clear()
End
Sub
Public
Shadows
Sub RemoveAt(ByVal
index As
Integer)
Remove(item(index))
End
Sub
End
Class