[VB.NET]With ステートメントでクラスのプロパティの初期値を指定する
書式
with オブジェクト名
.プロパティ1 = 値1
.プロパティ2 =値2
xxx
End With
with オブジェクト名
.プロパティ1 = 値1
.プロパティ2 =値2
xxx
End With
with オブジェクト名 .プロパティ1 = 値1 .プロパティ2 =値2 xxx End With
Dim リスト名 As New List(Of クラス名)
リスト名.Add(オブジェクト名)
使用例
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)
'インスタンスを生成
Dim usrA As New UserInfo()
'Withステートメントでプロパティを指定
With usrA
.userId = "1001"
.username = "山田 太郎"
End With
usrLst.Add(usrA)
'インスタンスを生成
Dim usrB As New UserInfo()
'Withステートメントでプロパティを指定
With usrB
.userId = "2001"
.username = "山田 二郎"
End With
usrLst.Add(usrB)
Console.WriteLine("リストの1番目要素")
Console.WriteLine("ユーザー名: " + usrLst(0).username + " ユーザーID:" + usrLst(0).userId)
Console.WriteLine("")
Console.WriteLine("リストの2番目要素")
Console.WriteLine("ユーザー名: " + usrLst(1).username + " ユーザーID:" + usrLst(1).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)
'インスタンスを生成
Dim usrA As New UserInfo()
'Withステートメントでプロパティを指定
With usrA
.userId = "1001"
.username = "山田 太郎"
End With
usrLst.Add(usrA)
'インスタンスを生成
Dim usrB As New UserInfo()
'Withステートメントでプロパティを指定
With usrB
.userId = "2001"
.username = "山田 二郎"
End With
usrLst.Add(usrB)
Console.WriteLine("リストの1番目要素")
Console.WriteLine("ユーザー名: " + usrLst(0).username + " ユーザーID:" + usrLst(0).userId)
Console.WriteLine("")
Console.WriteLine("リストの2番目要素")
Console.WriteLine("ユーザー名: " + usrLst(1).username + " ユーザーID:" + usrLst(1).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) 'インスタンスを生成 Dim usrA As New UserInfo() 'Withステートメントでプロパティを指定 With usrA .userId = "1001" .username = "山田 太郎" End With usrLst.Add(usrA) 'インスタンスを生成 Dim usrB As New UserInfo() 'Withステートメントでプロパティを指定 With usrB .userId = "2001" .username = "山田 二郎" End With usrLst.Add(usrB) Console.WriteLine("リストの1番目要素") Console.WriteLine("ユーザー名: " + usrLst(0).username + " ユーザーID:" + usrLst(0).userId) Console.WriteLine("") Console.WriteLine("リストの2番目要素") Console.WriteLine("ユーザー名: " + usrLst(1).username + " ユーザーID:" + usrLst(1).userId) Console.ReadKey() End Sub End Module
実行結果
リストの1番目要素
ユーザー名: 山田 太郎 ユーザーID:1001
リストの2番目要素
ユーザー名: 山田 二郎 ユーザーID:2001