Visual Basic サンプル集 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Visual Basic 中学校 > Visual Basic サンプル集 > Visual Basic サンプル集目次 >
プロパティの宣言
2021/3/28
目次
一般的なプロパティ
Public Property MyProp As String
メモ:この構文を自動実装プロパティと呼びます。
初期値も設定できます。
Public Property MyProp As String = "初期値"
メモ:System.ComponentModel.DefaultValue 属性 も参考。→ 属性の効果
読み取り専用プロパティ
Public ReadOnly Property MyProp As String ="初期値"
メモ:読み取り専用の自動実装プロパティに値を設定するには、初期値を記述するか、コンストラクターを使用します。
VB2013以前の場合、次のようにします。
Private myPropValue As String = "初期値"
Public ReadOnly Property MyProp As String
Get
Return myPropValue
End Get
End Property
初期化時のみ値を設定できるプロパティ
VBでは初期化時のみ値を設定できるプロパティを定義できません。C#ではinit専用セッターを使って定義できます。
処理を実行するプロパティ
この FilePathプロパティには実在しないパスを設定しようとすると例外を発生させます。
FilePathプロパティが設定されていないか、設定後削除されたなどの場合は、マイドキュメントのパスを表します。
Private filePathValue As String
Public Property FilePath As String
Get
If Not IO.Directory.Exists(filePathValue) Then
'パスが存在しない場合 マイドキュメントを使用します。VB2003以前の場合 SpecialFolder.Personal を使用してください。
Return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
End If
Return filePathValue
End Get
Set(value As String)
If IO.Directory.Exists(value) Then
filePathValue = value
Else
Throw New InvalidOperationException("存在しないパスは指定できません。")
End If
End Set
End Property
値の設定と読み取りでアクセスレベルが異なるプロパティ
次のプロパティは値の取得は Public で、値の設定は Protected です。
Private myPropValue As String
Public Property MyProp As String
Get
Return myPropValue
End Get
Protected Set(value As String)
myPropValue = value
End Set
End Property
式形式のプロパティ
VBでは式形式のプロパティを定義できません。
引数付きのプロパティ
次の CharsプロパティはUnicode文字列で指定した位置の文字を表します。
Public Class UnicodeString
Public Property Text As String
Public Sub New(text As String)
Me.Text = text
End Sub
''' <summary>
''' 指定された位置にある文字を表します。
''' </summary>
Public ReadOnly Property Chars(index As Integer) As String
Get
Dim itor = Globalization.StringInfo.GetTextElementEnumerator(Me.Text)
Do
itor.MoveNext()
Loop While itor.ElementIndex < index
Return itor.Current.ToString
End Get
End Property
End Class
メモ:この例では ReadOnly にしていますが、これは必須ではありません。
呼び出し例
Dim unicode As New UnicodeString("明日は🗻Let's Go!")
Dim c1 As String = unicode.Chars(0) '明
Dim c2 As String = unicode.Chars(1) '日
Dim c3 As String = unicode.Chars(2) 'は
Dim c4 As String = unicode.Chars(3) '🗻
Dim c5 As String = unicode.Chars(4) 'L
既定のプロパティ
引数付きのプロパティにDefaultキーワードを追加すると既定のプロパティになります。C#ではインデクサーに相当する機能です。
Public Class UnicodeString
Public Property Text As String
Public Sub New(text As String)
Me.Text = text
End Sub
''' <summary>
''' 指定された位置にある文字を表します。
''' </summary>
Public Default ReadOnly Property Chars(index As Integer) As String
Get
Dim itor = Globalization.StringInfo.GetTextElementEnumerator(Me.Text)
Do
itor.MoveNext()
Loop While itor.ElementIndex < index
Return itor.Current.ToString
End Get
End Property
End Class
メモ:この例では ReadOnly にしていますが、これは必須ではありません。
呼び出し例
明示的に Chars と記述しなくても Charsプロパティを呼び出せる点が既定のプロパティの特徴です。
Dim unicode As New UnicodeString("明日は🗻Let's Go!")
Dim c1 As String = unicode(0) '明
Dim c2 As String = unicode(1) '日
Dim c3 As String = unicode(2) 'は
Dim c4 As String = unicode(3) '🗻
Dim c5 As String = unicode(4) 'L
VB2008以前で一般的だったプロパティ定義方法
VB2008以前は自動実装プロパティが使用できないため、単純に値を保持するプロパティでも次のように書く必要があります。
Private myPropValue As String
Public Property MyProp As String
Get
Return myPropValue
End Get
Set(value As String)
myPropValue = value
End Set
End Property
VB6では Property Get ~ End Property, Property Let ~ End
Property を使用します。 →
VB6
初級講座 第28回 プロパティを作る