VB.Net Tutorial/Statements/For Each — различия между версиями

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

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

For Each with Collection

Imports System.Collections

public class Test
   public Shared Sub Main
        Dim m_employees As New Collection
        m_employees.Add(New employee("A", "F", "F"))
        m_employees.Add(New employee("B", "E", "H"))
        m_employees.Add(New employee("C", "D", "I"))

        For Each emp1 As employee In m_employees
            If emp1.FirstName = "A" Then Continue For
            Console.WriteLine(emp1.Title & " " & emp1.FirstName & " " & emp1.LastName)
        Next emp1
   End Sub
End class
    
Public Class Employee
    Public FirstName As String
    Public LastName As String
    Public Title As String
    Public Sub New(ByVal first_name As String, ByVal last_name As String, ByVal new_title As String)
        FirstName = first_name
        LastName = last_name
        Title = new_title
    End Sub
End Class
H B E
I C D

Use For Each to loop through Array

Option Strict On
 Imports System
 Public Class Employee
     Private empID As Integer
     Public Sub New(ByVal empID As Integer)
         Me.empID = empID
     End Sub 
     Public Overrides Function ToString( ) As String
         Return empID.ToString( )
     End Function 
 End Class 
 
 
 Class Tester
     Shared Sub Main( )
    
         Dim intArray( ) As Integer
         Dim empArray( ) As Employee
         intArray = New Integer(5) {}
         empArray = New Employee(3) {}
    
         Dim i As Integer
         For i = 0 To empArray.Length - 1
             empArray(i) = New Employee(i + 5)
         Next i
    
         Console.WriteLine("The Integer array...")
         Dim intValue As Integer
         For Each intValue In intArray
             Console.WriteLine(intValue.ToString( ))
         Next
    
         Console.WriteLine("The employee array...")
         Dim e As Employee
         For Each e In empArray
             Console.WriteLine(e)
         Next
     End Sub "Run
 End Class
The Integer array...
0
0
0
0
0
0
The employee array...
5
6
7
8