「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
'構造体Customerの定義
Structure Customer
'String型変数の宣言
Public userName As String
'Integer型変数の宣言
Public age As Integer
End Structure
Sub Main()
Dim cst As Customer
'Withと構造体の変数を設定
With cst
.userName = "yamada"
.age = 34
End With
Console.WriteLine("ユーザー名: " + cst.userName)
Console.WriteLine("年齢: " + cst.age.ToString)
Console.ReadKey()
End Sub
End Module
Module Module1
'構造体Customerの定義
Structure Customer
'String型変数の宣言
Public userName As String
'Integer型変数の宣言
Public age As Integer
End Structure
Sub Main()
Dim cst As Customer
'Withと構造体の変数を設定
With cst
.userName = "yamada"
.age = 34
End With
Console.WriteLine("ユーザー名: " + cst.userName)
Console.WriteLine("年齢: " + cst.age.ToString)
Console.ReadKey()
End Sub
End Module
Module Module1 '構造体Customerの定義 Structure Customer 'String型変数の宣言 Public userName As String 'Integer型変数の宣言 Public age As Integer End Structure Sub Main() Dim cst As Customer 'Withと構造体の変数を設定 With cst .userName = "yamada" .age = 34 End With Console.WriteLine("ユーザー名: " + cst.userName) Console.WriteLine("年齢: " + cst.age.ToString) Console.ReadKey() End Sub End Module
実行結果
ユーザー名: yamada
年齢: 34