Option Explicit
Private Const mlngENTRIESPERROW As Long = 4&
Private mstrName As String
Private mdblVcoord As Double
Private mdblScoord As Double
Private mdblZcoord As Double
Private mdblPCurrent As Double
Public Property Get Name() As String
Name = mstrName
End Property
Public Property Get v() As Double
v = mdblVcoord
End Property
Public Property Get s() As Double
s = mdblScoord
End Property
Public Property Get z() As Double
z = mdblZcoord
End Property
Public Property Get Current() As Double
Current = mdblPCurrent
End Property
' Extracts the ID, xcoord and ycoord component data from the CSV file row, strRow.
' strRow should match the format required for a City row.
' Returns True if the row and the data conform to the required format, False otherwise.
Public Function ReadDataFromRow(ByVal strRow As String) As Boolean
Dim strEntry As String
Const gstrCOMMA As String = ","
ReadDataFromRow = False
If CountDelimitedEntries(strRow, gstrCOMMA) <> mlngENTRIESPERROW Then
Exit Function
End If
mstrName = DetachNextDelimitedEntry(strRow, gstrCOMMA)
If Len(mstrName) = 0 Then
Exit Function
End If
strEntry = DetachNextDelimitedEntry(strRow, gstrCOMMA)
If Not IsNumeric(strEntry) Then
Exit Function
End If
mdblVcoord = CDbl(strEntry)
strEntry = DetachNextDelimitedEntry(strRow, gstrCOMMA)
If Not IsNumeric(strEntry) Then
Exit Function
End If
mdblScoord = CDbl(strEntry)
strEntry = DetachNextDelimitedEntry(strRow, gstrCOMMA)
If Not IsNumeric(strEntry) Then
Exit Function
End If
mdblZcoord = CDbl(strEntry)
ReadDataFromRow = True
End Function
' Returns the number of delimiter-separated entries in strLine.
' Note that a final entry is presumed to exist after the final delimiter,
' so the result returned if effectively the number of delimiters, plus 1.
' The only exception is the empty string, for which a value of 0 is returned.
Public Function CountDelimitedEntries(ByVal strLine As String, ByVal strDelimiter As String) As Long
Dim lngPos As Long
Dim lngCount As Long
CountDelimitedEntries = 0&
If Len(strLine) = 0 Then
Exit Function
End If
strLine = strLine & strDelimiter
lngCount = 0
lngPos = 0
Do
lngCount = lngCount + 1
lngPos = InStr(lngPos + 1, strLine, strDelimiter, vbTextCompare)
Loop While lngPos < Len(strLine)
CountDelimitedEntries = lngCount
End Function
' Returns the substring from the start of strLine up to, but excluding, the
' first delimiter (or the whole string if no delimiters are found).
' Note that strLine is then stripped of the entry and the delimiter (if it exists)
Public Function DetachNextDelimitedEntry(ByRef strLine As String, ByVal strDelimiter As String) As String
Dim lngPos As Long
lngPos = InStr(strLine, strDelimiter)
If lngPos > 0 Then
DetachNextDelimitedEntry = Left$(strLine, lngPos - 1)
strLine = Mid$(strLine, lngPos + 1)
Else
DetachNextDelimitedEntry = strLine
strLine = ""
End If
End Function