Question Selecting field and then displaying results from the Comma delimited text

unoimgr8st

New member
Joined
Oct 11, 2011
Messages
1
Programming Experience
1-3
Hi,


I have a text file data.txt, which stores the information for social club and the information are stored like

655211, Male, David Graham, 1992, 20, david.g@gmail.com, 0411221122

I have got the code which displays all the info, however i need to modify it to display only male or females
Also, if you reckon there could be better code than this please do help me with that as qww.

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\data.txt")
Label1.Text = (fileReader)
 
Here's two approaches you can try?

*i used a listbox in these examples, but you get the idea.

1)If your using .net 3.5/4 you could do something like this with linq:

    Private fileFullPath As String = Environment.CurrentDirectory & "\data.txt"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim people As IEnumerable(Of String) = From line In IO.File.ReadAllLines(fileFullPath) _
                                              Where line.Split(",")(1).Trim = "Male" _
                                              Select line

        For Each person In people
            lstDisplay.Items.Add(person)
        Next
    End Sub



2)i feel like something like this would be more effective though? depends on your situation.
Create a structure/class for to represent a person.
Create a collection to hold person
Read all of the lines of the textfile into a string array
loop through each line
for each line split by delimiter
create a person
add person to collection

now you have a collection of people, loop through it to do whatever you need.
One thing i may suggest about the Textfile, if possible, perhaps put the column name as the first row, that way you can use that as a reference to know which column belongs to which property.

heres an example:

Public Class Form1
    Structure Person
        Property Id As Integer
        Property Gender As String
        Property FullName As String
        ''and so forth

        Public Overrides Function ToString() As String
            Return String.Format("ID: {0}, Name: {1}", Id, Gender, FullName)
        End Function
    End Structure

    Private people As New List(Of Person)
    Private fileFullPath As String = Environment.CurrentDirectory & "\data.txt"

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            loadTextFile()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub loadTextFile()
        Dim lines() As String = IO.File.ReadAllLines(fileFullPath)

        For Each line In lines
            Dim parts() As String = line.Split(",")

            If parts.Length = 7 Then
                Dim person As New Person With {.Id = parts(0).Trim, .Gender = parts(1).Trim, .FullName = parts(2).Trim} ' dont forget to add the other properties...
                'considering the length with 7 properties, it would probably look neater if you didnt use a with{} block to set the properties
                people.Add(person)

            Else
                Throw New ConstraintException("Must contain 7 columns")
            End If
        Next
    End Sub
    Private Sub populateWithGender(ByVal genderName As String)
        For Each Person In people
            If Person.Gender = genderName Then
                lstDisplay.Items.Add(Person.ToString)
            End If
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        lstDisplay.Items.Clear()
        populateWithGender("Male")
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        lstDisplay.Items.Clear()
        populateWithGender("Female")
    End Sub
End Class
 
Back
Top