Option Strict On
Imports System
Namespace InterfaceDemo
'define the interface
Interface IStorable
Sub Read()
Sub Write(ByVal obj As Object)
Property Status() As Integer
End Interface
'create a class that implements the IStorable interface
Public Class document
Implements IStorable
'oh cool it just added a bunch of things on its own
Private myStatus As Integer = 0
Public Sub New(ByVal s As String)
Console.WriteLine("Creaing document with: {0}", s)
End Sub 'new
Public Sub Read() Implements IStorable.Read
Console.WriteLine("Implementing the Read Method for IStorable")
End Sub 'read
Public Property Status As Integer Implements IStorable.Status
Get
Return myStatus
End Get
Set(ByVal value As Integer)
myStatus = value
End Set
End Property
Public Sub Write(ByVal obj As Object) Implements IStorable.Write
Console.WriteLine("Implementing the write method for IStorable.")
End Sub 'write
End Class
Class tester
Public Sub run()
Console.writeline("Implementing Tester.Run")
Dim doc As New document("Test Document")
doc.status = -1
doc.read()
console.writeline("Document status: {0} ", doc.status)
End Sub
End Class
End Namespace
Module Module1
Sub Main()
Dim t As New InterfaceDemo.tester()
t.run()
Console.WriteLine("Ran an instance of InterfaceDemo.tester()")
Console.ReadLine()
End Sub
End Module
Classes can implement any number of interfaces.
Option Strict On
Imports System
Namespace InterfaceDemo
Interface IStorable
Sub Read()
Sub Write(ByVal obj As Object)
Property status() As Integer
End Interface 'IStorable
Interface ICompressible
Sub Compress()
Sub Decompress()
End Interface 'ICompressible
'document implements both interfaces
Public Class Document
Implements ICompressible, IStorable
'hold the value for IStorable's Status property
Private myStatus As Integer = 0
'the document constructor
Public Sub New(ByVal s As String)
Console.WriteLine("Creating document with {0} ", s)
End Sub 'New
Public Sub Compress() Implements ICompressible.Compress
Console.WriteLine("Implementing Compress")
End Sub
Public Sub Decompress() Implements ICompressible.Decompress
Console.WriteLine("Implementing Decompress")
End Sub
Public Sub Read() Implements IStorable.Read
Console.WriteLine("Implementing the read method for IStorable")
End Sub 'Read
Public Property status As Integer Implements IStorable.status
Get
Return myStatus
End Get
Set(ByVal value As Integer)
myStatus = value
End Set
End Property
Public Sub Write(ByVal obj As Object) Implements IStorable.Write
Console.WriteLine("Implementing the Write Method for IStorable")
End Sub 'Write
End Class
End Namespace
And here is a variation I wrote:
Class Tester
Public Sub Run(ByVal textValue As String)
Dim doc As New Document(textValue)
doc.status = -1
doc.Read()
doc.Compress()
Console.WriteLine("Document Status: {0}", doc.status)
End Sub 'Run
End Class
End Namespace
Module Module1
Sub Main()
Dim textVal As String = ""
For i = 1 To 5
textVal = "Document #" & i.ToString
Dim t As New InterfaceDemo.Tester()
t.Run(textVal)
Next
Console.ReadLine()
End Sub 'main
End Module
No comments:
Post a Comment