다음을 통해 공유


방법: 항목 목록 만들기

이 항목의 코드는 Student 클래스를 정의하고 클래스의 인스턴스 목록을 만듭니다. 이 목록은 연습: Visual Basic에서 쿼리 작성 항목을 지원하기 위해 디자인되었습니다. 또한 개체 목록을 필요로 하는 모든 응용 프로그램에 대해서도 사용할 수 있습니다. 코드는 개체 이니셜라이저를 사용하여 학생 목록에서 항목을 정의합니다.

예제

연습 중인 경우 연습에서 만든 프로젝트의 Module1.vb 파일에 이 코드를 사용할 수 있습니다. Main 메서드에서 ****로 표시된 줄을 연습에 나와 있는 쿼리 및 쿼리 실행으로 바꾸십시오.

Module Module1

    Sub Main()
        ' Create a list of students.
        Dim students = GetStudents()
        ' Display the names in the list.
        DisplayList(students)
        ' ****Paste query and query execution code from the walkthrough,
        ' ****or any code of your own, here in Main.
        Console.ReadLine()
    End Sub

    ' Call DisplayList to see the names of the students in the list.
    ' You can expand this method to see other student properties.
    Sub DisplayList(ByVal studentCol As IEnumerable(Of Student))
        For Each st As Student In studentCol
            Console.WriteLine("First Name: " & st.First)
            Console.WriteLine(" Last Name: " & st.Last)
            Console.WriteLine()
        Next
    End Sub

    ' Function GetStudents returns a list of Student objects.
    Function GetStudents() As IEnumerable(Of Student)
        Return New List(Of Student) From
            {
             New Student("Michael", "Tucker", "Junior", 10),
             New Student("Svetlana", "Omelchenko", "Senior", 2),
             New Student("Michiko", "Osada", "Senior", 7),
             New Student("Sven", "Mortensen", "Freshman", 53),
             New Student("Hugo", "Garcia", "Junior", 16),
             New Student("Cesar", "Garcia", "Freshman", 4),
             New Student("Fadi", "Fakhouri", "Senior", 72),
             New Student("Hanying", "Feng", "Senior", 11),
             New Student("Debra", "Garcia", "Junior", 41),
             New Student("Lance", "Tucker", "Junior", 60),
             New Student("Terry", "Adams", "Senior", 6)
            }
    End Function

    ' Each student has a first name, a last name, a class year, and 
    ' a rank that indicates academic ranking in the student body.
    Public Class Student
        Public Property First As String
        Public Property Last As String
        Public Property Year As String
        Public Property Rank As Integer

        Public Sub New(ByVal firstName As String,
                       ByVal lastName As String,
                       ByVal studentYear As String,
                       ByVal studentRank As Integer)
            First = firstName
            Last = lastName
            Year = studentYear
            Rank = studentRank
        End Sub
    End Class
End Module

참고 항목

작업

연습: Visual Basic에서 쿼리 작성

개념

개체 이니셜라이저: 명명된 형식과 익명 형식(Visual Basic)

Visual Basic의 LINQ 소개

기타 리소스

Video How to: Writing Queries in Visual Basic

Visual Basic에서 LINQ 시작

Visual Basic의 LINQ

쿼리(Visual Basic)