Public Interface インターフェース名 関連付けるプロパティやメソッドを記述する(型も含む)。 End Interface
まず、クラスの宣言文の次の行で、インターフェースに関連付けるための定義を行う。
Implements インターフェース名
次に、インターフェースを通じて公開するプロパティやメソッドにおいて、インターフェースに関連付けるための定義を行う。
Property プロパティ名() As データ型 Implements インターフェース名.プロパティ名 … End Property
SubあるいはFunction メソッド名() As データ型 Implements インターフェース名.メソッド名 … End Property
1:新規フォームForm1を作成し、次のコントロールを配置する。
2:同じプロジェクトのところに、Class1.vbを追加する。中身は次のようなコードにする。
Public Interface myInterface Property Name() As String Property Birthday() As String Function GetAge() As Integer End Interface Pulic Class Class1 Implements myInterface Private strName As String Private datBirthday As Date Public Property Name() As String Implements myInterface.Name Get Return strName EndGet Set(ByVal value As String) strName = value End Set End Property Public Property Birthday() As String Implements myInterface.Birthday Get Return datBirthday EndGet Set(ByVal value As String) datBirthday = value End Set End Property Public Function GetAge() As Integer Implements myInterface.GetAge Return Int(DateTime.Today.Subtract(Birthday).Days / 365.25) End Property End Public
3:Button1をクリックしたときのイベントハンドラを次のように記述する。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim person As myInterface = New Class1() Dim iPlug As myInterface = person iPlug.Name = TextBox1.Text iPlug.Birthday = DateTimePicker1.Value.Date MessageBox.Show(iPlug.Nmae & "の年齢=" & iPlug.GetAge()) End Sub