VB.Net by API/System.Collections.Generic/Dictionary — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:50, 26 мая 2010

Dictionary.Add

  
 
Option Strict On
Imports System.Collections.Generic
Public Module modMain
   Private employees As Dictionary(Of String, Employee)
   Public Sub Main()
      employees = New Dictionary(Of String, Employee)
      employees.Add("name",New Employee("name", "1"))
      employees.Add("name1",New Employee("name1", "1"))
      Console.WriteLine("There are {0} employees now on file. They are:",employees.Count)
      For Each pair As KeyValuePair(Of String, Employee) In Employees
         Console.WriteLine(" {0}: {1}", pair.Key, pair.Value.Name)
      Next
   End Sub
      
End Module
Public Class Employee
   Private empName As String
   Private empID As String
   Public Sub New(name As String, ID As String)
      Me.empName = name
      Me.empID = ID
   End Sub
   Public Property Name() As String
      Get
         Return Me.empName
      End Get
      Set
         Me.empName = Value
      End Set
   End Property
   Public Property ID As String
      Get
         return Me.empID
      End Get
      Set
         Me.empID = Value
      End Set
   End Property
End Class


Dictionary.Item

  
Imports System.Collections.Generic
Public Class GenericTest
   Public Shared Sub Main()
      Dim education As New Dictionary(Of String, String)
      education.Add("BA", "Bachelor of Arts")
      education.Add("BS", "Bachelor of Science")
      education.Add("MA", "Master of Arts")
      education.Add("MS", "Master of Science")
      education.Add("MPhil", "Master of Philosophy")
      education.Add("PhD", "Doctor of Philosophy")
      Console.WriteLine(education.Item("BS"))
      Console.WriteLine()
      For Each pair As KeyValuePair(Of String, String) In education
         Console.WriteLine(pair.Value)
      Next
   End Sub
End Class


New Dictionary

  
Imports System.Collections.Generic
Public Structure Employee
   Dim Name As String
   Dim HireDate As Date
   Dim BirthDate As Date
End Structure
Public Module Test
   Public Sub Main()
      Dim employees As New Dictionary(of String, Employee)
      Dim dateToFind As Date
      Dim input As String
      Dim emp As New Employee
      emp.Name = "S"
      emp.HireDate = #1/2/2003#
      emp.BirthDate = #7/12/1977#
      employees.Add(emp.Name, emp)
      emp.Name = "A"
      emp.HireDate = #8/18/1999#
      emp.BirthDate = #3/16/1964#
      employees.Add(emp.Name, emp)
      emp.Name = "B"
      emp.HireDate = #3/1/1987#
      emp.BirthDate = #11/12/1955#
      employees.Add(emp.Name, emp)

      dateToFind = CDate("7/12/1977")
      For Each employee As KeyValuePair(of String, Employee) In employees
         If Month(employee.Value.BirthDate) = Month(dateToFind) AndAlso Day(employee.Value.BirthDate) = Day(dateToFind) Then
            Console.WriteLine("{0:MMMM d} is the bithday of {1}.", dateToFind, employee.Key)
         End If
         
         If Month(employee.Value.HireDate) = Month(dateToFind) AndAlso Day(employee.Value.HireDate) = Day(dateToFind) Then
            Console.WriteLine("{0:MMMM d} is the hiring anniversary of {1}.", dateToFind, employee.Key)
         End If
      Next
   End Sub
End Module