次の方法で共有


方法 : 項目のリストを作成する

更新 : 2007 年 11 月

このトピックのコードでは、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)
        Dim studentList As New System.Collections.Generic.List(Of Student)
        Dim student0 As New Student With {.First = "Michael", _
                                          .Last = "Tucker", _
                                          .Year = "Junior", _
                                          .Rank = 10}
        Dim student1 As New Student With {.First = "Svetlana", _
                                          .Last = "Omelchenko", _
                                          .Year = "Senior", _
                                          .Rank = 2}
        Dim student2 As New Student With {.First = "Michiko", _
                                          .Last = "Osada", _
                                          .Year = "Senior", _
                                          .Rank = 7}
        Dim student3 As New Student With {.First = "Sven", _
                                          .Last = "Mortensen", _
                                          .Year = "Freshman", _
                                          .Rank = 53}
        Dim student4 As New Student With {.First = "Hugo", _
                                          .Last = "Garcia", _
                                          .Year = "Junior", _
                                          .Rank = 16}
        Dim student5 As New Student With {.First = "Cesar", _
                                          .Last = "Garcia", _
                                          .Year = "Freshman", _
                                          .Rank = 4}
        Dim student6 As New Student With {.First = "Fadi", _
                                          .Last = "Fakhouri", _
                                          .Year = "Senior", _
                                          .Rank = 72}
        Dim student7 As New Student With {.First = "Hanying", _
                                          .Last = "Feng", _
                                          .Year = "Senior", _
                                          .Rank = 11}
        Dim student8 As New Student With {.First = "Debra", _
                                          .Last = "Garcia", _
                                          .Year = "Junior", _
                                          .Rank = 41}
        Dim student9 As New Student With {.First = "Lance", _
                                          .Last = "Tucker", _
                                          .Year = "Junior", _
                                          .Rank = 60}
        Dim student10 As New Student With {.First = "Terry", _
                                           .Last = "Adams", _
                                           .Year = "Senior", _
                                           .Rank = 6}
        studentList.Add(student0)
        studentList.Add(student1)
        studentList.Add(student2)
        studentList.Add(student3)
        studentList.Add(student4)
        studentList.Add(student5)
        studentList.Add(student6)
        studentList.Add(student7)
        studentList.Add(student8)
        studentList.Add(student9)
        studentList.Add(student10)
        Return studentList
    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
        Private _first As String
        Public Property First() As String
            Get
                Return _first
            End Get
            Set(ByVal value As String)
                _first = value
            End Set
        End Property
        Private _last As String
        Public Property Last() As String
            Get
                Return _last
            End Get
            Set(ByVal value As String)
                _last = value
            End Set
        End Property
        Private _year As String
        Public Property Year() As String
            Get
                Return _year
            End Get
            Set(ByVal value As String)
                _year = value
            End Set
        End Property
        Private _rank As Integer
        Public Property Rank() As Integer
            Get
                Return _rank
            End Get
            Set(ByVal value As Integer)
                _rank = value
            End Set
        End Property
    End Class
End Module

参照

処理手順

チュートリアル : Visual Basic でのクエリの作成

概念

オブジェクト初期化子 : 名前付きの型と匿名型

Visual Basic における LINQ の概要

その他の技術情報

Visual Basic の LINQ の概要

LINQ (Visual Basic)

クエリ (Visual Basic)