0

I have a file containing the first name, second name and age of a person:

John, Smith, 35
Andrew, Jacobs, 52

I have split up the first name, second name and age and put them into arrays.

Becuase I know that there are 2 people (I set the array size to 1) , I am using the following 'for' loop:

For x = 0 to 1
textbox1.text = textbox1.text + first_name(x)
Next

I want to change this to the following:

For x = 0 to however long the file is
textbox1.text = textbox1.text + first_name(x)
Next

However, when I try to use 0 to first_name.length it doesn't work.

  • 1
    "Doesn't work" can mean many things. Narrow it down in an [edit] to help those trying to understand. –  Mar 30 '15 at 16:21
  • 3
    Arrays are zero based, the end is at first_name.Length - 1 – Steve Mar 30 '15 at 16:21
  • 1
    you already knew that: `I know that there are 2 people (I set the array size to 1)` – Ňɏssa Pøngjǣrdenlarp Mar 30 '15 at 16:23
  • When doing first_name.Length -1 I get An unhandled exception of type 'System.NullReferenceException' Additional information: Object reference not set to an instance of an object. –  Mar 30 '15 at 16:25
  • Welcome to Stack Overflow! Nearly all NullReference Exceptions have the same set of causes. See [NullReference Exception in Visual Basic](http://stackoverflow.com/a/26761773/1070452) for help on this. – Ňɏssa Pøngjǣrdenlarp Mar 30 '15 at 16:30
  • Now you have changed the problem still without showing how do you read the file and split it. This is the central point of your problems but without that code is like chasing a ghost – Steve Mar 30 '15 at 16:33
  • edit your post with the relevant code - no one wants to read that – Ňɏssa Pøngjǣrdenlarp Mar 30 '15 at 16:42
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Mar 30 '15 at 16:43

3 Answers3

2

If what you want to do is put the contents of a line-delimited file into a VB string array, why don't you just let the file tell you how many lines there are?

Like:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim listOfPeople As String()
    listOfPeople = System.IO.File.ReadAllLines("C:\\Temp\\file.txt")

    For i As Integer = 0 To listOfPeople.Length - 1
        Console.WriteLine(listOfPeople(i))
        Dim person As String = listOfPeople(i)
        Dim personAttributes As String() = person.Split(",")
        For j As Integer = 0 To personAttributes.Length - 1
            ' do something with the attribute
        Next
    Next

End Sub
Gojira
  • 2,941
  • 2
  • 21
  • 30
  • however i have got 3 arrays –  Mar 30 '15 at 16:30
  • You have one list of people, who each have multiple attributes (name, etc. ) correct? Then you loop through the lines in the file and do something with each line. That 'something' might be split it into its own array. – Gojira Mar 30 '15 at 16:31
  • For i As Integer = 0 To myArray.Length - 1temp = myArray(i).Split(",") First_name(i) = temp(0) second_name(i) = temp(1) age(i) = temp(2) i = i + 1 Next –  Mar 30 '15 at 16:45
  • I re-wrote the code sample to use easier-to-understand variable names, and included an inner loop that takes into account splitting the line into an array based on comma-delimited values. – Gojira Mar 30 '15 at 16:49
1

How about creating a Person class - then you won't have separate arrays to carry around.

Try this:

Public Class Person
    Public FirstName As String
    Public LastName As String
    Public Age As Integer
End Class

Public Function ReadPeople(filePath As String) As Person()
    Return (From record In IO.File.ReadAllLines(filePath)
            Let Terms = record.Split(","c)
            Select New Person With {.LastName = Terms(0), .FirstName = Terms(1), .Age = CInt(Terms(2))}) _
            .ToArray
End Function

Usage:

    Dim people = ReadPeople("c:\people.txt")
Craig Johnson
  • 744
  • 4
  • 8
0

If I were you I would just make things simple as possible.

Try this:

textbox1.Text = _
    String.Join("", File.ReadAllLines("") _
        .Select(Function(x) x.Split(","C)(0).Trim()))
Enigmativity
  • 113,464
  • 11
  • 89
  • 172