「VB.NET」オブジェクトにWith~End Withを使う
書式
With objectExpression
[ statements ]
End With
With 変数(クラスのオブジェクト)
処理コード
End With
With objectExpression
[ statements ]
End With
With 変数(クラスのオブジェクト)
処理コード
End With
With objectExpression [ statements ] End With With 変数(クラスのオブジェクト) 処理コード End With
オブジェクトまたは構造のメンバーにアクセスする場合にステートメントで簡単な構文を使用できるように、単一のオブジェクトまたは構造を繰り返し参照する一連のステートメントを実行します。
使用例
Module Module1
Public Class Customer
'String型変数の宣言
Private userName As String
'List型変数の宣言
Private comLst As New List(Of String)
'String型 usernameのget setメソッド
Public Property Name() As String
Get
Return userName
End Get
Set(ByVal value As String)
userName = value
End Set
End Property
'リスト型comLstのget setメソッド
Public Property Comments() As List(Of String)
Get
Return comLst
End Get
Set(ByVal value As List(Of String))
comLst = value
End Set
End Property
End Class
Sub Main()
'インスタンスの生成
Dim theCus As New Customer
'String型変数の値を設定
With theCus
.Name = "テスト太郎"
End With
'List型変数の値を設定
With theCus.Comments
.Add("東京")
.Add("大阪")
.Add("福岡")
End With
Console.WriteLine("ユーザ名:" + theCus.Name)
Dim resLst As List(Of String)
resLst = theCus.Comments
Console.WriteLine("オブジェクトのメンバーリスト型の要素")
For Each cft In resLst
Console.WriteLine(cft)
Next
Console.ReadKey()
End Sub
End Module
Module Module1
Public Class Customer
'String型変数の宣言
Private userName As String
'List型変数の宣言
Private comLst As New List(Of String)
'String型 usernameのget setメソッド
Public Property Name() As String
Get
Return userName
End Get
Set(ByVal value As String)
userName = value
End Set
End Property
'リスト型comLstのget setメソッド
Public Property Comments() As List(Of String)
Get
Return comLst
End Get
Set(ByVal value As List(Of String))
comLst = value
End Set
End Property
End Class
Sub Main()
'インスタンスの生成
Dim theCus As New Customer
'String型変数の値を設定
With theCus
.Name = "テスト太郎"
End With
'List型変数の値を設定
With theCus.Comments
.Add("東京")
.Add("大阪")
.Add("福岡")
End With
Console.WriteLine("ユーザ名:" + theCus.Name)
Dim resLst As List(Of String)
resLst = theCus.Comments
Console.WriteLine("オブジェクトのメンバーリスト型の要素")
For Each cft In resLst
Console.WriteLine(cft)
Next
Console.ReadKey()
End Sub
End Module
Module Module1 Public Class Customer 'String型変数の宣言 Private userName As String 'List型変数の宣言 Private comLst As New List(Of String) 'String型 usernameのget setメソッド Public Property Name() As String Get Return userName End Get Set(ByVal value As String) userName = value End Set End Property 'リスト型comLstのget setメソッド Public Property Comments() As List(Of String) Get Return comLst End Get Set(ByVal value As List(Of String)) comLst = value End Set End Property End Class Sub Main() 'インスタンスの生成 Dim theCus As New Customer 'String型変数の値を設定 With theCus .Name = "テスト太郎" End With 'List型変数の値を設定 With theCus.Comments .Add("東京") .Add("大阪") .Add("福岡") End With Console.WriteLine("ユーザ名:" + theCus.Name) Dim resLst As List(Of String) resLst = theCus.Comments Console.WriteLine("オブジェクトのメンバーリスト型の要素") For Each cft In resLst Console.WriteLine(cft) Next Console.ReadKey() End Sub End Module
実行結果
ユーザ名:テスト太郎
オブジェクトのメンバーリスト型の要素
東京
大阪
福岡