[VB.NET]クラスを複数のオブジェクトをリストに格納するサンプル
書式
Dim オブジェクト名 As New List(Of クラス名)
オブジェクト名.Add(New クラス名 With {.プロパティ1 = 値1, xxxx})
使い方
リスト名(インデックス).プロパティ
使用例
Module Module1
'クラスの定義
Public Class UserInfo
'String型の宣言
Public userId As String
Public username As String
'userIdのget setメソッド
Public Property id() As String
Get
Return userId
End Get
Set(ByVal value As String)
userId = value
End Set
End Property
'usernameのget set メソッド
Public Property name() As String
Get
Return username
End Get
Set(ByVal value As String)
username = value
End Set
End Property
End Class
Sub Main()
Dim usrLst As New List(Of UserInfo)
'オブジェクト初期化子を使用してクラスのインスタンスを生成 インスタンスをリストにセット
usrLst.Add(New UserInfo With {.userId = "1001", .username = "山田"})
'要素2のデータを追加
usrLst.Add(New UserInfo With {.userId = "2001", .username = "大崎"})
'要素3のデータをリストに追加
usrLst.Add(New UserInfo With {.userId = "3001", .username = "杉山"})
'要素4のデータを追加
usrLst.Add(New UserInfo With {.userId = "4001", .username = "室蘭"})
Console.WriteLine("リストの2番目要素クラスのプロパティを出力")
Console.WriteLine("ユーザー名: " + usrLst(1).username + " ユーザーID:" + usrLst(1).userId)
Console.WriteLine("リストの3番目要素クラスのプロパティを出力")
Console.WriteLine("ユーザー名: " + usrLst(2).username + " ユーザーID:" + usrLst(2).userId)
Console.ReadKey()
End Sub
End Module
Module Module1
'クラスの定義
Public Class UserInfo
'String型の宣言
Public userId As String
Public username As String
'userIdのget setメソッド
Public Property id() As String
Get
Return userId
End Get
Set(ByVal value As String)
userId = value
End Set
End Property
'usernameのget set メソッド
Public Property name() As String
Get
Return username
End Get
Set(ByVal value As String)
username = value
End Set
End Property
End Class
Sub Main()
Dim usrLst As New List(Of UserInfo)
'オブジェクト初期化子を使用してクラスのインスタンスを生成 インスタンスをリストにセット
usrLst.Add(New UserInfo With {.userId = "1001", .username = "山田"})
'要素2のデータを追加
usrLst.Add(New UserInfo With {.userId = "2001", .username = "大崎"})
'要素3のデータをリストに追加
usrLst.Add(New UserInfo With {.userId = "3001", .username = "杉山"})
'要素4のデータを追加
usrLst.Add(New UserInfo With {.userId = "4001", .username = "室蘭"})
Console.WriteLine("リストの2番目要素クラスのプロパティを出力")
Console.WriteLine("ユーザー名: " + usrLst(1).username + " ユーザーID:" + usrLst(1).userId)
Console.WriteLine("リストの3番目要素クラスのプロパティを出力")
Console.WriteLine("ユーザー名: " + usrLst(2).username + " ユーザーID:" + usrLst(2).userId)
Console.ReadKey()
End Sub
End Module
Module Module1 'クラスの定義 Public Class UserInfo 'String型の宣言 Public userId As String Public username As String 'userIdのget setメソッド Public Property id() As String Get Return userId End Get Set(ByVal value As String) userId = value End Set End Property 'usernameのget set メソッド Public Property name() As String Get Return username End Get Set(ByVal value As String) username = value End Set End Property End Class Sub Main() Dim usrLst As New List(Of UserInfo) 'オブジェクト初期化子を使用してクラスのインスタンスを生成 インスタンスをリストにセット usrLst.Add(New UserInfo With {.userId = "1001", .username = "山田"}) '要素2のデータを追加 usrLst.Add(New UserInfo With {.userId = "2001", .username = "大崎"}) '要素3のデータをリストに追加 usrLst.Add(New UserInfo With {.userId = "3001", .username = "杉山"}) '要素4のデータを追加 usrLst.Add(New UserInfo With {.userId = "4001", .username = "室蘭"}) Console.WriteLine("リストの2番目要素クラスのプロパティを出力") Console.WriteLine("ユーザー名: " + usrLst(1).username + " ユーザーID:" + usrLst(1).userId) Console.WriteLine("リストの3番目要素クラスのプロパティを出力") Console.WriteLine("ユーザー名: " + usrLst(2).username + " ユーザーID:" + usrLst(2).userId) Console.ReadKey() End Sub End Module
実行結果
リストの2番目要素クラスのプロパティを出力
ユーザー名: 大崎 ユーザーID:2001
リストの3番目要素クラスのプロパティを出力
ユーザー名: 杉山 ユーザーID:3001