Home > Blockchain >  What type of array should I use for a database of information about the user? vb.net
What type of array should I use for a database of information about the user? vb.net

Time:12-08

So I'm trying to make a system where a person can input data about different people and I want to store it in a textile to search and sort the data, It would contain first name, surname, location, phone number.

If I were to use a 2d array how would I go about inputting the data through a loop instead of just writing it out myself.

Or I could use one dimensional arrays and store each type of data, first name, surname etc in separate text files but I am not sure if that would work/be efficient.

CodePudding user response:

If you stored the data as JSON, it would be easier to maintain.

You would create a class to represent the data, read in the file, parse the contents to a collection of your class, and then use LINQ to do your filtering.

This would be efficient enough if you aren't doing any inserts/updates, in which case I would recommend moving to a database to handle these. You could still use the same approach and just serialize the JSON collection back to the file, but you would start to see some significant slow down of the write operation pretty quickly.

Here is an example of defining the class:

Public Class Person
    <JsonProperty("firstName")>
    Public Property FirstName As String

    <JsonProperty("surname")>
    Public Property Surname As String

    <JsonProperty("location")>
    Public Property Location As String

    <JsonProperty("phoneNumber")>
    Public Property PhoneNumber As String

End Class

Here is an example of reading a text file and parsing it to a collection of your class:

Public Function GetPeople(path As String) As IEnumerable(Of Person)
    Dim literal = IO.File.ReadAllText(path)
    Dim people = JsonConverter.DeserializeObject(Of IEnumerable(Of Person))(literal)

    Return people
End Function

Here is an example of adding people to the collection and serializing it back to the file:

Public Function AddPerson(path As String, record As Person) As IEnumerable(Of Person)
    Dim people = GetPeople(path).ToList()
    people.Add(record)
    IO.File.WriteAllText(path, people.ToString())
    Return people
End Function

Keep in mind you'd want to add exception handling and business logic to check that the state is what you would expect it to be.

  • Related