Torna al Thread
'//Classe che mi rappresenta la riga del file CSV
Public Class CSV_Object
'//I valori delle colonne del file CSV
Private s_strVal1 As String = ""
Private s_strVal2 As String = ""
Private s_strVal3 As String = ""
Private s_strVal4 As String = ""
Private s_strVal5 As String = ""
Private s_strVal6 As String = ""
'//Il costruttore della classe
Public Sub New(ByVal s_strValues() As String)
'//Verifico se il parametro passato al costruttore no è nullo
If Not s_strValues Is Nothing Then
'//Verifico che abbia almeno 6 campi
If s_strValues.Length = 6 Then
'//In caso affermativo procedo con la valorizzazione delle variabili della classe
s_strVal1 = s_strValues(0)
s_strVal2 = s_strValues(1)
s_strVal3 = s_strValues(2)
s_strVal4 = s_strValues(3)
s_strVal5 = s_strValues(4)
s_strVal6 = s_strValues(5)
End If
End If
End Sub
'//Proprietà pubbliche in sola lettura per recuperare i valori dall'oggetto appena creato
Public ReadOnly Property GetValore1() As String
Get
Return Me.s_strVal1
End Get
End Property
Public ReadOnly Property GetValore2() As String
Get
Return Me.s_strVal2
End Get
End Property
Public ReadOnly Property GetValore3() As String
Get
Return Me.s_strVal3
End Get
End Property
Public ReadOnly Property GetValore4() As String
Get
Return Me.s_strVal4
End Get
End Property
Public ReadOnly Property GetValore5() As String
Get
Return Me.s_strVal5
End Get
End Property
Public ReadOnly Property GetValore6() As String
Get
Return Me.s_strVal6
End Get
End Property
End Class
Private o_ListaOggetti As New Collections.ObjectModel.Collection(Of CSV_Object)
Private Sub ParseCSV(ByVal s_strFileName As String, Optional ByVal s_strSeparator As String = ";")
Dim FP As StreamReader = Nothing
Dim o_ListaOggetti As New Collections.ObjectModel.Collection(Of CSV_Object)
'//Inizializzo la lista
o_ListaOggetti.Clear()
Try
'//Verifico se il file esiste, in caso affermativo procedo con l'elaborazione
If System.IO.File.Exists(s_strFileName) Then
FP = New StreamReader(s_strFileName)
'//Leggo il file fino alla fine
While Not FP.EndOfStream
Dim s_strParams() As String = Split(FP.ReadLine, s_strSeparator)
'//Creo un nuovo oggetto di tipo CSV_Object, con i parametri recuperati dal CSV e la aggiungo alla lista "o_ListaOggetti"
o_ListaOggetti.Add(New CSV_Object(s_strParams))
End While
'//Chiudo il file CSV
FP.Close()
End If
Catch ex As Exception
'//In caso di errore visualizzo un messaggio d'errore!
MsgBox(ex.Message)
End Try
End Sub