locked
Vb: Array And For Loop RRS feed

  • Question

  • User-1437709844 posted

    hi! I am quite a noob in ASP.Net.

    I have to create a textbox which will list down one column of datas from textfile which is separate by delimters.
    But as I have to filter alt.Row from the text file, what can i do?

    Will i be using array and for loop?

     

    thanks! 

    Monday, July 12, 2010 11:51 PM

Answers

  • User1006193418 posted

    Hi,

    Your code seems to be OK. Is there any problem you find in read the value from text file?

    I also suggest you having a try on this code which is modified based on your version that it uses StringBuilder instead of string.

            Dim output As New StringBuilder
    
            Try
                Dim sr As New StreamReader("")
                Dim line As String = sr.ReadLine
    
                Do While Not line Is Nothing
    
                    If IsNumeric(line.Chars(0)) Then
                        Dim arr() As String = Split(line, ";")
    
                        If arr.Length > 3 Then
                            output.AppendLine(arr(0))
                            output.AppendLine(arr(3))
                            output.AppendLine("")
                        End If
    
                    End If
    
                    line = sr.ReadLine
                Loop
    
    
            Catch ex As Exception
    
            End Try

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 16, 2010 7:02 AM
  • User1006193418 posted

    I need to skip a line in the textfile..how to i code it so as i able to read alt lines from the textfile?(:

    Hi,

    You just need to add another line of code like this.

            Dim output As New StringBuilder
    
            Try
                Dim sr As New StreamReader("")
                Dim line As String = sr.ReadLine
    
                Do While Not line Is Nothing
    
                    If IsNumeric(line.Chars(0)) Then
                        Dim arr() As String = Split(line, ";")
    
                        If arr.Length > 3 Then
                            output.AppendLine(arr(0))
                            output.AppendLine(arr(3))
                            output.AppendLine("")
                        End If
    
                    End If
    
                    line = sr.ReadLine
                    line = sr.ReadLine       <--
                Loop
    
    
            Catch ex As Exception
    
            End Try

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 18, 2010 10:29 PM

All replies

  • User-68639941 posted

    hi,

    refer below code

    Dim sr As New StreamReader("c:\hello.txt") 
      Dim values() As String = sr.ReadToEnd().Split(",")  // read text file and split by delimiter and put into string array
      Dim index As Integer = 0
      For index = 1 To 10 Step 2 // reading from array and bypassing the alternate index
       Response.Write(values(index))
      Next

     

    Tuesday, July 13, 2010 1:17 AM
  • User-1437709844 posted

    hi!

    What if my codings is like this?how can i sub it into here?

    Thanksss!

     

    Dim

    b, c, d As String

     

    Try

     

     

     

    Dim sr As New StreamReader("C:\Temp\datalogger\" & date1.ToString & "\int_sma_" + date1.ToString + ".txt")Dim line6 = sr.ReadLine'Dim values() As String = sr.ReadToEnd().Split(";") ' read text file and split by delimiter and put into string array

     

    'Dim index As Integer = 0

     

    Do While Not line6 Is Nothing 'loop until end of file

     

     

     

    If IsNumeric(line6.Chars(0)) Then

     

     

    b += fields5(0) + vbCr + fields5(20) + vbCr +

    Dim fields5() = Split(line6, ";")" "

    c += fields5(20) + vbCr

     

    'End If

     

    'For b = 1 To 10 Step 2

     

    'Next

     

    End If

    line6 = sr.ReadLine

     

    Loop

     

    'For index = 1 To 10 Step 2 ' reading from array and bypassing the alternate index

     

    'Response.Write(values(index))

     

    '

     

    'Next

     

     

     

    Catch ex As ExceptionEnd Try

    TextBox1.Text = b

    TextBox2.Text = c

     

    End Sub

    Tuesday, July 13, 2010 11:38 PM
  • User-1437709844 posted

    hi!

    What if my codings is like this?how can i sub it into here?

    Thanksss!

     

    Dim

    b, c, d As String

     

    Try

     

     

     

    Dim sr As New StreamReader("C:\Temp\datalogger\" & date1.ToString & "\int_sma_" + date1.ToString + ".txt")Dim line6 = sr.ReadLine'Dim values() As String = sr.ReadToEnd().Split(";") ' read text file and split by delimiter and put into string array

     

    'Dim index As Integer = 0

     

    Do While Not line6 Is Nothing 'loop until end of file

     

     

     

    If IsNumeric(line6.Chars(0)) Then

     

     

    b += fields5(0) + vbCr + fields5(20) + vbCr +

    Dim fields5() = Split(line6, ";")" "

    c += fields5(20) + vbCr

     

    'End If

     

    'For b = 1 To 10 Step 2

     

    'Next

     

    End If

    line6 = sr.ReadLine

     

    Loop

     

    'For index = 1 To 10 Step 2 ' reading from array and bypassing the alternate index

     

    'Response.Write(values(index))

     

    '

     

    'Next

     

     

     

    Catch ex As ExceptionEnd Try

    TextBox1.Text = b

    TextBox2.Text = c

     

    End Sub

    Tuesday, July 13, 2010 11:39 PM
  • User-952121411 posted

    Can you please use the code formatter (the little button that looks like '{}' in the editor), select VB.NET as the language, and repaste your code?  It is difficult to read in the last (2) posts. 

    Thursday, July 15, 2010 8:41 AM
  • User-1437709844 posted
    Dim b As String
    
            Try
                Dim sr As New StreamReader("C:\Temp\datalogger\" & date1.ToString & "\int_ana_" + date1.ToString + ".txt")
                Dim line6 = sr.ReadLine
    
               
                Do While Not line6 Is Nothing 'loop until end of file
               
                    If IsNumeric(line6.Chars(0)) Then
    
    
                        Dim fields5() = Split(line6, ";")
                       
                        b += fields5(0) + vbCr + fields5(3) + vbCr + " "
                    
    
                        
                        
    
                       
                    End If
                    line6 = sr.ReadLine
                Loop
                'For index = 1 To 10 Step 2 ' reading from array and bypassing the alternate index
                
            Catch ex As Exception
    
            End Try
    
    
            TextBox1.Text = b


     

    Thursday, July 15, 2010 9:19 PM
  • User1006193418 posted

    Hi,

    Your code seems to be OK. Is there any problem you find in read the value from text file?

    I also suggest you having a try on this code which is modified based on your version that it uses StringBuilder instead of string.

            Dim output As New StringBuilder
    
            Try
                Dim sr As New StreamReader("")
                Dim line As String = sr.ReadLine
    
                Do While Not line Is Nothing
    
                    If IsNumeric(line.Chars(0)) Then
                        Dim arr() As String = Split(line, ";")
    
                        If arr.Length > 3 Then
                            output.AppendLine(arr(0))
                            output.AppendLine(arr(3))
                            output.AppendLine("")
                        End If
    
                    End If
    
                    line = sr.ReadLine
                Loop
    
    
            Catch ex As Exception
    
            End Try

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 16, 2010 7:02 AM
  • User-1437709844 posted

    hi!

    I need to skip a line in the textfile..how to i code it so as i able to read alt lines from the textfile?(:


    Thanks! 

    Sunday, July 18, 2010 9:36 PM
  • User1006193418 posted

    I need to skip a line in the textfile..how to i code it so as i able to read alt lines from the textfile?(:

    Hi,

    You just need to add another line of code like this.

            Dim output As New StringBuilder
    
            Try
                Dim sr As New StreamReader("")
                Dim line As String = sr.ReadLine
    
                Do While Not line Is Nothing
    
                    If IsNumeric(line.Chars(0)) Then
                        Dim arr() As String = Split(line, ";")
    
                        If arr.Length > 3 Then
                            output.AppendLine(arr(0))
                            output.AppendLine(arr(3))
                            output.AppendLine("")
                        End If
    
                    End If
    
                    line = sr.ReadLine
                    line = sr.ReadLine       <--
                Loop
    
    
            Catch ex As Exception
    
            End Try

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 18, 2010 10:29 PM
  • User-1437709844 posted

    Thanks Alot! It Works!

    Thanks! 

    Monday, August 2, 2010 12:30 AM