Public Class Entry
Private _ID As String
Private _Name As String
Private _Number1 As Single
Private _Number2 As Single
Public Property ID() As String
Get
Return _ID
End Get
Set(ByVal Value As String)
_ID = Value
End Set
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property Number1() As Single
Get
Return _Number1
End Get
Set(ByVal Value As Single)
_Number1 = Value
End Set
End Property
Public Property Number2() As Single
Get
Return _Number2
End Get
Set(ByVal Value As Single)
_Number2 = Value
End Set
End Property
Public Sub New()
MyBase.new()
End Sub
Public Shared Function Parse(ByVal line As String) As Entry
'I switched the constructor to a shared parse method
'which seemed better since it is really just a parser anyway
Try
Dim parts() As String = line.Split(Char.Parse(","))
Dim newEntry As New Entry
newEntry.ID = parts(0)
newEntry.Name = parts(1)
newEntry.Number1 = CType(parts(2), Single)
newEntry.Number2 = CType(parts(3), Single)
Return newEntry
Catch ex As Exception
Throw New ArgumentException("Invalid data!")
End Try
End Function
Public Shared Function GetEntriesFromFile(ByVal filepath As String) As Entry()
Dim fs As New IO.FileStream(filepath, IO.FileMode.Open)
Dim sr As New IO.StreamReader(fs)
'temp storage for results
Dim al As New ArrayList
Do While sr.Peek > 0
'make an entry object for every line
Dim item As Entry = Entry.Parse(sr.ReadLine)
al.Add(item)
Loop
fs.Close()
'convert back to a regular array of entry objects
Return CType(al.ToArray(GetType(Entry)), Entry())
End Function
End Class
'syntax
ListBox1.DisplayMember = "Name"
ListBox1.DataSource = Entry.GetEntriesFromFile("data.txt")