Reputation: 375
could someone show me how to search my CSV file for two values? My CSV File looks like this:
1,"Garry","Tall","4545"
2,"Julius", "Short", "2564"
And I want to confirm that the 4545 number matches the name Garry in the same row. For example, a user may enter 4545 and the name Garry and the code would check if the csv matches those two values. But the name and number must match, not just a single value.
I'm not sure on how to load the csv file into my visual basic or how to search it. So any help would be greatly appreciated. I have been looking around online for the past two hours but nothing seems to be working for me so far.
Public Function CheckRegistrationKey(ByVal Name As String, ByVal Number As Integer)
Dim filepath As String = My.Computer.FileSystem.SpecialDirectories.Desktop("\File1.csv")
Dim Key As String
Dim NameToSearhFor As String = Name
Dim NumberToSearchFor As Integer = Number
'Load file
'Search file for values
'return true if found
'return false if not found
End Function
Update
'Load file
Using MyReader As New FileIO.TextFieldParser(filepath)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
'MsgBox(currentField)
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
End Try
End While
End Using
Now I just need to work out how to search the values.
Upvotes: 0
Views: 2330
Reputation: 25023
If you only ever need to check one user once then your proposed code will suffice, but if you need to check the users more than once then something more like this could be better:
Option Infer On
Option Strict On
Module Module1
Dim data As List(Of datum)
Public Class datum
Property ID As Integer
Property FirstName As String
Property LastName As String
Property Key As Integer
End Class
Function LoadData(srcFile As String) As List(Of datum)
Dim data As New List(Of datum)
Using tfp As New FileIO.TextFieldParser(srcFile)
tfp.TextFieldType = FileIO.FieldType.Delimited
tfp.SetDelimiters(",")
tfp.HasFieldsEnclosedInQuotes = True
Dim currentRow As String()
Dim currentLine = 0
While Not tfp.EndOfData
currentLine += 1
Try
currentRow = tfp.ReadFields()
If currentRow.Length = 4 Then
data.Add(New datum With {.ID = CInt(currentRow(0)), .FirstName = currentRow(1), .LastName = currentRow(2), .Key = CInt(currentRow(3))})
End If
Catch ex As Exception
MsgBox($"Error in file {srcFile} at line {currentLine}: {ex.Message}")
End Try
End While
End Using
Return data
End Function
Function IsValidUser(firstname As String, key As Integer) As Boolean
If data Is Nothing Then
Throw New Exception("Data has not been initialised.")
End If
Return data.Any(Function(d) d.FirstName = firstname AndAlso d.Key = key)
End Function
Sub Main()
Dim srcFile = "C:\temp\sampledata2.txt"
data = LoadData(srcFile)
For Each user In {"Garry", "Harry"}
Console.WriteLine($"Found {user}: " & IsValidUser(user, 4545).ToString())
Next
Console.ReadLine()
End Sub
End Module
Upvotes: 1
Reputation: 375
It might not be the most efficent way, but I've got my code to work as detailed below.
'Load file
Using MyReader As New FileIO.TextFieldParser(filepath)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim LastName As String = currentRow(1)
Dim Key As String = currentRow(4)
If LastName = "Garry" And Key = "6565" Then
'id correct
Else
'id wrong
End If
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
'Error code here
End Try
End While
End Using
Upvotes: 0