我定义并实现了接口,但在使用时(运行程序),却出现"未将对象引用设置到对象的实例。"可是"New是不能在接口上使用的"
这个问题怎么解决啊.
我红、绿、蓝代码是书上抄的,黑色的是自己写的,但出却出错了。
Public Interface Iconnectable
Function Connect() As Boolean
Sub Disconnect()
End Interface
Public Interface ITimedConnectable
Inherits Iconnectable
Sub TimeOut(ByVal Millisecs As Long)
End Interface
Public Class Class1
Implements ITimedConnectable
Implements IDisposable
Public Function Connect() As Boolean Implements Iconnectable.Connect
End Function
Public Sub Disconnect() Implements Iconnectable.Disconnect
End Sub
Public Sub TimeOut(ByVal Millisecs As Long) Implements ITimedConnectable.TimeOut
End Sub
Public Sub SendMessage(ByVal msg As String)
Console.Write("输出信息:{0}", msg)
End Sub
#Region " IDisposable Support "
' Visual Basic 添加此代码是为了正确实现可处置模式。
Public Sub Dispose() Implements IDisposable.Dispose
' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean) 中。
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
'*************************
Private disposedValue As Boolean = False ' 检测冗余的调用
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: 显式调用时释放非托管资源
End If
' TODO: 释放共享的非托管资源
End If
Me.disposedValue = True
End Sub
Sub MyGemericRoutine(ByVal tc As ITimedConnectable)
tc.TimeOut(5000)
tc.Connect()
tc.Disconnect()
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.CliDim MyClass1 As New Class1
Dim TC As ITimedConnectable
TC.TimeOut(5000)
TC.Connect()
TC.Disconnect()
End Sub