I have visual basic 2010 windows application assignment in which I have to populate a one dimensional array from a text file stored in the projects debug folder. The text file is 6 five digit numbers on seperate lines. One number was typed then enter was pressed 6 times. The following is copied and pasted from the text file:
25000
35000
55000
70000
80200
90500
I have to populate a one dimensional array. This is the declaration I am using: Dim intSalaries() As Integer. I'm new to this and taking an advanced visual basic programming course on line and struggling. Any help will be appreciated.

Note that this does no error checking - it assumes that all of the strings represent numbers.

        Dim strings() As String = System.IO.File.ReadAllLines("D:\temp\test.txt")
        Dim numbers(UBound(strings)) As Integer

        For i = 0 To UBound(strings)
            numbers(i) = CInt(strings(i))
        Next

A version that checks but does not strictly speaking use an array is

        Dim numbers As New List(Of Integer)

        For Each line As String In System.IO.File.ReadAllLines("D:\temp\test.txt")
            If IsNumeric(line) Then
                numbers.Add(CInt(line))
            End If
        Next

Thank myou for posting so quickly. The actual assignment is to modify the following code:

Public Class frmMain

Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
    ' displays the salary amount associated with a code

    Dim intSalaries() As Integer = {25000, 35000, 55000, 70000, 80200, 90500}

    Dim strInputCode As String
    Dim intCode As Integer
    Dim intSub As Integer

    strInputCode = InputBox("Salary code (1 - 6)", "Salary")
    Integer.TryParse(strInputCode, intCode)
    ' subtract 1 from the code to get the appropriate subscript
    intSub = intCode - 1

    If intSub >= 0 AndAlso intSub < intSalaries.Length Then
        lblSalary.Text = intSalaries(intSub).ToString("C2")
    Else
        lblSalary.Text = String.Empty
        MessageBox.Show("The salary code must be from 1 through 6.",
                        "Salary", MessageBoxButtons.OK,
                        MessageBoxIcon.Information)
    End If

End Sub

End Class

In this version the array is declared with the salary amount given. There is a button(btnDisplay) and a label for output. I am to modify the code to populate the array from a text file(salary.txt) stored in the debug folder of the solution. The inputbox asks for a code(1-6) and the number corresponds to the position of the numbers stored in the array. Once again, thanks for your help. - Ben

You can use the second form (List). Just replace intSalaries.Length with intSalaries.Count as in

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

        ' displays the salary amount associated with a code

        Dim intSalaries As New List(Of Integer)

        For Each line As String In System.IO.File.ReadAllLines("D:\temp\test.txt")
            If IsNumeric(line) Then
                intSalaries.Add(CInt(line))
            End If
        Next

        Dim strInputCode As String
        Dim intCode As Integer
        Dim intSub As Integer

        strInputCode = InputBox("Salary code (1 - 6)", "Salary")
        Integer.TryParse(strInputCode, intCode)

        ' subtract 1 from the code to get the appropriate subscript

        intSub = intCode - 1
        If intSub >= 0 AndAlso intSub < intSalaries.Count Then
            lblSalary.Text = intSalaries(intSub).ToString("C2")
        Else
            lblSalary.Text = String.Empty
            MessageBox.Show("The salary code must be from 1 through 6.",
            "Salary", MessageBoxButtons.OK,
            MessageBoxIcon.Information)
        End If

    End Sub

Except you probably don't want to read the values every time the button is clicked. You'd initialize the List once on Form Load.

Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
' displays the salary amount associated with a code
Dim intSalaries As New List(Of Integer)
For Each line As String In System.IO.File.ReadAllLines("D:\temp\test.txt")
If IsNumeric(line) Then
intSalaries.Add(CInt(line))
End If
Next
Dim strInputCode As String
Dim intCode As Integer
Dim intSub As Integer
strInputCode = InputBox("Salary code (1 - 6)", "Salary")
Integer.TryParse(strInputCode, intCode)
' subtract 1 from the code to get the appropriate subscript
intSub = intCode - 1
If intSub >= 0 AndAlso intSub < intSalaries.Count Then
lblSalary.Text = intSalaries(intSub).ToString("C2")
Else
lblSalary.Text = String.Empty
MessageBox.Show("The salary code must be from 1 through 6.",
"Salary", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End Sub

Worked perfectly. Thank you. That's what's up!

Hi,

Since you are a student, I wanted to show you two other methods for reading a text file as you have described. The second one is just for fun; I would not recommend it.

For your needs the ReadAllLines method shown by Jim is fine, but if the file is large you are unnecessarily storing a large amount of text. Additionally the maximum size of .Net object (string) is 2 GB (1Gig of unicode characters), so it could become an issue.

   Private ValueArray() As Int32
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim values As New List(Of Int32)
      Dim fs As IO.Stream = IO.File.OpenRead("data.txt") 'open file in the executable folder
      Dim sr As New IO.StreamReader(fs) 'create a stream reader to read the file
      Dim line As String
      While sr.Peek >= 0
         Try
            line = sr.ReadLine
            values.Add(CType(line, Int32))
         Catch ex As Exception
            'skip line
            'could display message about bad data
            'MsgBox("bad data:  " & line)
         End Try
      End While
      sr.Close()

      'put the values into the array per your assignment
      ValueArray = values.ToArray
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      'for a blast from the past
      Dim values As New List(Of Int32)
      Dim fn As Int32 = Microsoft.VisualBasic.FreeFile
      Microsoft.VisualBasic.FileOpen(fn, "data.txt", OpenMode.Input)
      Dim line As String
      While Not Microsoft.VisualBasic.EOF(fn)
         Try
            line = Microsoft.VisualBasic.LineInput(fn)
            values.Add(CType(line, Int32))
         Catch ex As Exception
            'skip line
            'could display message about bad data
            'MsgBox("bad data:  " & line)
         End Try
      End While
      Microsoft.VisualBasic.FileClose(fn)

      'put the values into the array per your assignment
      ValueArray = values.ToArray
   End Sub

Thank you for the knowledge. I am bookmarking this page because we have a final coming up and we are allowed to use whatever resources we want. This is gold to me.

So if you did want to initialize on the load event like Jim had said, how would you go about doing that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.