(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Add key value pair to Collection
Option Strict On
Imports System.Collections
Public Class Collect
Public Shared Sub Main()
Dim sta As New Collection
sta.Add("New York", "NY")
sta.Add("Michigan", "MI")
sta.Add("New Jersey", "NJ")
sta.Add("Massachusetts", "MA")
For Each stcode As String In sta
Console.WriteLine(stcode)
Next
End Sub
End Class
New York
Michigan
New Jersey
Massachusetts
Deleting the members of a collection
Option Strict On
Public Module CollectionTest
Public Sub Main()
Dim weights As New Collection
weights.Add("ounces", "oz")
weights.Add("pounds", "lbs")
weights.Add("kilograms", "kg")
weights.Add("milligrams", "mg")
For ordinal As Integer = weights.Count To 1 Step -1
weights.Remove(ordinal)
Next
Console.WriteLine("The collection now has {0} items.", weights.Count)
End Sub
End Module
The collection now has 0 items.
Get Enumerator from Collection
Imports System.Collections
Public Class Enumerate
Public Shared Sub Main()
Dim weights As New Collection
Dim iterator As IEnumerator
weights.Add("ounces", "oz")
weights.Add("pounds", "lbs")
weights.Add("kilograms", "kg")
weights.Add("milligrams", "mg")
iterator = weights.GetEnumerator
Do While iterator.MoveNext
Console.WriteLine(iterator.Current)
Loop
End Sub
End Class
ounces
pounds
kilograms
milligrams
Inserting items into a collection by index
Public Class Tester
Public Shared Sub Main
Dim wordCollection As New Collection
wordCollection.Add("This")
wordCollection.Add("is")
wordCollection.Add("a")
wordCollection.Add("collection")
wordCollection.Add("of")
wordCollection.Add("words")
" ----- Insert a word after item 3.
wordCollection.Add("slightly", , , 3)
" ----- Insert a word before item 5.
wordCollection.Add("longer", , 5)
For Each word As String In wordCollection
Console.WriteLine(word)
Next word
End Sub
End Class
This
is
a
slightly
longer
collection
of
words
Remove from Collection
public class Test
public Shared Sub Main
Dim employees As New Collection
employees.Add(New Employee("A"))
employees.Add(New Manager("B"))
employees.Add(New Manager("C"))
employees.Add(New Employee("D"))
For Each emp As Employee In employees
Console.WriteLine(emp.Name)
Next emp
For i As Integer = employees.Count To 1 Step -1
Dim emp As Employee = CType(employees(i), Employee)
If emp.IsManager Then employees.Remove(i)
Next i
For Each emp As Employee In employees
Console.WriteLine(emp.Name)
Next emp
End Sub
End class
Public Class Employee
Public Name As String
Public Sub New(ByVal new_name As String)
Name = new_name
End Sub
Public Overridable Function IsManager() As Boolean
Return False
End Function
End Class
Public Class Customer
Public Name As String
Public Sub New(ByVal new_name As String)
Name = new_name
End Sub
End Class
Public Class Manager
Inherits Employee
Public Sub New(ByVal new_name As String)
MyBase.new(new_name)
End Sub
Public Overrides Function IsManager() As Boolean
Return True
End Function
End Class
A
B
C
D
A
D
Scanning items in a collection
Public Class Tester
Public Shared Sub Main
Dim numberCollection As New Collection
numberCollection.Add(14, "C")
numberCollection.Add(25, "D")
numberCollection.Add(36, "E")
numberCollection.Add(47, "A")
numberCollection.Add(58, "B")
" ----- Scan the collection with a loop counter.
" Collections are base-1, not base-0.
For counter As Integer = 1 To numberCollection.Count
Console.WriteLine(numberCollection(counter))
Next counter
" ----- Scan the collection by item.
For Each number As Integer In numberCollection
Console.WriteLine(number)
Next number
" ----- Retrieve items by key.
Console.WriteLine(numberCollection("A"))
Console.WriteLine(numberCollection("B"))
Console.WriteLine(numberCollection("C"))
Console.WriteLine(numberCollection("D"))
Console.WriteLine(numberCollection("E"))
End Sub
End Class
14
25
36
47
58
14
25
36
47
58
47
58
14
25
36
Store objects in Collection and retrieve by key and index
Imports System
Public Class Employee
Private myEmpID As Integer
Public Sub New(ByVal empID As Integer)
Me.myEmpID = empID
End Sub "New
Public Overrides Function ToString( ) As String
Return myEmpID.ToString( )
End Function "ToString
Public Property EmpID( ) As Integer
Get
Return myEmpID
End Get
Set(ByVal Value As Integer)
myEmpID = Value
End Set
End Property
End Class "Employee
Class Tester
Shared Sub Main( )
Dim intCollection As New Collection( )
Dim empCollection As New Collection( )
Dim empCollection2 As New Collection( )
Dim i As Integer
For i = 0 To 4
empCollection.Add(New Employee(i + 100))
intCollection.Add((i * 5))
Next i
empCollection2.Add(New Employee(1), "G")
empCollection2.Add(New Employee(2), "J")
empCollection2.Add(New Employee(3), "T")
For Each i In intCollection
Console.Write("{0} ", i.ToString( ))
Next i
Console.WriteLine( )
Console.WriteLine("Employee collection...")
Dim e As Employee
For Each e In empCollection
Console.Write("{0} ", e.ToString( ))
Next e
Console.WriteLine( )
Console.WriteLine("Employee collection 2...")
For Each e In empCollection2
Console.Write("{0} ", e.ToString( ))
Next e
Console.WriteLine( )
Dim emp As Employee
emp = empCollection2.Item("J")
Console.WriteLine(emp.ToString( ))
emp = empCollection2.Item(1)
Console.WriteLine("Index(1) retrieved empID {0}", emp.ToString( ))
End Sub
End Class
0 5 10 15 20
Employee collection...
100 101 102 103 104
Employee collection 2...
1 2 3
2
Index(1) retrieved empID 1