「VB.NET」With~End Withでクラスのオブジェクトをインスタンス化する
書式
Dim オブジェクト名 As New クラス名 With オブジェクト名 .プロパティ1 =値1 xxx End With
クラスをインスタンス化する時に、初期値も設定できます。
使用例
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 usrA As New UserInfo()
'Withステートメントでプロパティを指定
With usrA
.userId = "40004"
.username = "テスト 太郎"
End With
Console.WriteLine("クラスをインスタンス化する結果")
Console.WriteLine("ユーザー名: " + usrA.username + " ユーザーID:" + usrA.userId)
Console.ReadKey()
End Sub
End Module
結果
クラスをインスタンス化する結果
ユーザー名: テスト 太郎 ユーザーID:40004