Hi,
i am try to read from a file and then split the data and store them in array
but i am getting an error. can someone please point to the right direction

here is the file i am trying to read from

8
223432,YNYYYYYYYYYNYNYNYYNY-
623123,YNYNYNYYNY--YNYNYYNY
912234,YYYNYNYNYNYYNYYNYYNY
612356,YYYNYFYNYYNYYNYYNYYN
165478,YYYNYYYYY-NYYNYYNYYN
612321,YYYNYFYNYYNYYNYYNYYN
165213,YYYNYYYYY-NYYNYYNYYN

my code is as below

Dim Result As DialogResult 
		Dim strFileName As String
		Dim sreStreamReader As IO.StreamReader
		Dim strRecord As String
		Dim strRecord1 As string
		Dim strField(1) As string 
		Dim intCount As Integer = 0
		Dim mstrName(7) As string
	        Dim mintMark(7) As string
		
		Try
			Result = OpenStudentResponsesFile.ShowDialog
			If (Result = Windows.Forms.DialogResult.OK) Then
				strFileName =OpenStudentResponsesFile.FileName 
				'open file
				sreStreamReader = IO.File.OpenText(strFileName)
				strRecord = sreStreamReader.ReadLine()& vbNewLine
				MessageBox.Show (strRecord)
				'read file
				While (sreStreamReader.Peek <> -1)
				strRecord1 = sreStreamReader.ReadLine()
				
				strField = strRecord1.Split(",")
				
				
				mstrName(intCount) = strField(0)
				mintMark(intCount) =strField(1)				
				intCount = intCount + 1
				lbldisplay.Text=strField(1)
				End While
				sreStreamReader.Close()
				
			End If
			
		Catch ex As Exception
			MessageBox.Show (ex.Message)
		End Try
	End Sub

Recommended Answers

All 3 Replies

Use List(OF ) generics instead of Arrays.

....
        Dim list1 As New List(Of String)
        Dim list2 As New List(Of String)

        Dim lines As String() = System.IO.File.ReadAllLines("sample.txt")

        For Each Str As String In lines
            Dim arr As String() = Str.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
            list1.Add(arr(0))
            list2.Add(arr(1))
        Next
  ....

Thanks,

In my same program i am trying to display the result on a second form, but i am having problem getting it done. can someone please point me to the right direction.

Thanks,

Thanks,

In my same program i am trying to display the result on a second form, but i am having problem getting it done. can someone please point me to the right direction.

Thanks,

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.