Talstra

Talstra

Shifting Focus...

Array

Fundamentals of OOP in VB.NET

Interfaces

Estimated reading: 1 minute 170 views Contributors

In VB.NET, an interface is a contract that specifies a set of members that a class must implement. To define an interface in VB.NET, we use the Interface keyword. A class that implements an interface must implement all the members defined in that interface.

Example:

vb.netCopy codeInterface IShape
    ReadOnly Property Area As Double
    ReadOnly Property Perimeter As Double
End Interface

Class Rectangle
    Implements IShape
    
    Private _length As Double
    Private _width As Double
    
    Public Sub New(ByVal length As Double, ByVal width As Double)
        Me._length = length
        Me._width = width
    End Sub
    
    Public ReadOnly Property Area As Double Implements IShape.Area
        Get
            Return Me._length * Me._width
        End Get
    End Property
    
    Public ReadOnly Property Perimeter As Double Implements IShape.Perimeter
        Get
            Return 2 * (Me._length + Me._width)
        End Get
    End Property
End Class

' Create an object of the Rectangle class and access its properties
Dim rectangle As New Rectangle(5, 10)
Console.WriteLine(rectangle.Area)
Console.WriteLine
Share this Doc

Interfaces

Or copy link

CONTENTS
Chat Icon Close Icon