我已經實現IDataReader的和需要的功能的類中寫的,但我得到一個錯誤信息,說明如何在不重新輸入所有方法的情況下實現IDataReader?
班「CSVDataReader」必須實現「功能GetBoolean(我作爲整數)爲布爾」的接口「系統.Data.IDataRecord」。
IDataReader有很多功能和屬性。如何在不重寫所有功能的情況下實現這些功能?
這裏是我
類Public Class CSVDataReader
Implements IDataReader
Private stream As StreamReader
Private columnsByName As New Dictionary(Of String, Integer)()
Private columnsByOrdinal As New Dictionary(Of Integer, String)()
Private currentRow As String()
Private _isClosed As Boolean = True
Public Sub New(fileName As String)
If Not File.Exists(fileName) Then
Throw New Exception("File [" & fileName & "] does not exist.")
End If
Me.stream = New StreamReader(fileName)
Dim headers As String() = stream.ReadLine().Split(",")
For i As Integer = 0 To headers.Length - 1
columnsByName.Add(headers(i), i)
columnsByOrdinal.Add(i, headers(i))
Next
_isClosed = False
End Sub
Public Sub Close()
If stream IsNot Nothing Then
stream.Close()
End If
_isClosed = True
End Sub
Public ReadOnly Property FieldCount() As Integer
Get
Return columnsByName.Count
End Get
End Property
''' <summary>
''' This is the main function that does the work - it reads in the next line of data and parses the values into ordinals.
''' </summary>
''' <returns>A value indicating whether the EOF was reached or not.</returns>
Public Function Read() As Boolean
If stream Is Nothing Then
Return False
End If
If stream.EndOfStream Then
Return False
End If
currentRow = stream.ReadLine().Split(",")
Return True
End Function
Public Function GetValue(i As Integer) As Object
Return currentRow(i)
End Function
Public Function GetName(i As Integer) As String
Return columnsByOrdinal(i)
End Function
Public Function GetOrdinal(name As String) As Integer
Return columnsByName(name)
End Function
Public Function GetOrdinal(name As String) As Integer
Return columnsByName(name)
End Function
End Class
在VS中,您應該能夠右鍵單擊代碼中的接口名稱,然後選擇「實現接口」。這將爲您排除所有接口方法。適用於C#,我認爲VB也支持它。 – Jason
不幸的是,VB沒有那麼多重構優點(至少我上次看過;也許後來的版本更好)。 – prprcupofcoffee