none
For Next statement RRS feed

  • Question

  • Hi All,

    Text File:

    6917,9058,0689,1337,8176,6711,3979,6195,9340,4421,0302,2605,5748,8390,3578,2928,9052,5627,9009,9876,4323,3573,4553
    1765,7819,7323,4484,0194,4250,5873,0890,4133,1558,2420,1589,6190,2116,1603,3035,6298,4620,1426,2135,9507,8118,0409
    9429,4624,8724,7704,3671,4701,4395,2168,3583,4832,7545,9270,9607,3931,2596,7327,0447,4277,9267,1724,1646,6181,0533
    2719,8619,1121,8176,6265,6792,4738,7083,4193,3013,7757,2923,3087,1162,0485,9365,7385,1742,0097,2836,4814,7867,3382
    9815,9594,0408,8995,9428,6689,4185,5083,1183,0055,9180,1507,6691,4791,3275,1575,0138,0733,8270,8267,4519,9989,8258
    3051,1234,9039,7497,0704,0162,8296,1305,0081,2846,5472,1659,0965,3184,5462,4227,0702,8244,8755,2636,7818,8516,2716
    3876,9285,8726,8746,4867,8341,6204,7663,4042,9559,5573,2298,5513,6082,0093,6128,1096,5894,5923,3195,3233,4542,0592
    7450,8356,4274,3185,2361,9267,4306,0213,8845,1668,0019,3443,7990,9451,7241,5625,1365,6489,3392,9023,9827,9730,2233

    I have two For ..Next satement in Button_1 and Button_2 Click.

    What I need ;

    If we find the "Line Number" and  and "Line_Data" in Click Button_1 then

    in Click Button_2 must show "Line Number + 1" and "Line_Data + 1".That mean Advance one Data.

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    
    Public Class Form1
        Dim DataLines As New List(Of myData)
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox1.Clear()
    
            Dim sr As New System.IO.StreamReader("C:\Users\family\Documents\C.txt")
    
            Do While sr.Peek <> -1
    
                Dim MyItems As New List(Of myItem)
                Dim StringToCheck As String = sr.ReadLine()
    
                For i As Integer = 0 To 9
                    Dim StringToFind As String = CStr(i)
                    Dim Result As Integer = StringToCheck.Count(Function(c As Char) c = StringToFind)
                    MyItems.Add(New myItem With {.Index = i, .Value = Result})
                Next
                'If sr.Peek = -1 Then Exit Sub
    
                Dim Digits As New myData With {.Line_Data = StringToCheck}
                DataLines.Add(Digits)
                'TextBox1.AppendText(vbCrLf)
    
            Loop
    
            For I As Integer = 0 To DataLines.Count - 1
                TextBox1.AppendText("Line Number = " & I.ToString & vbCrLf)
                TextBox1.AppendText(DataLines(I).Line_Data & vbCrLf)
            Next
    
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            For I As Integer = 0 To DataLines.Count - 1
    
            Next
    
    
            'For I As Integer = 0 To DataLines.Count Step 2
            'TextBox2.AppendText(I.ToString & vbCrLf & DataLines(I).Line_Data & vbCrLf)
            'Next
    
        End Sub
    End Class
    Public Class myItem
        Public Index As Integer
        Public Value As Integer
        Public Overrides Function ToString() As String
            Return Index.ToString & " => " & Value.ToString
        End Function
    End Class
    
    Public Class myData
    
        Public Line_Data As String
    
    
    End Class

    Thank All.


    • Edited by mipakteh Wednesday, August 30, 2017 8:49 AM Add
    Wednesday, August 30, 2017 8:47 AM

Answers

  • If you have to suggest a better code.

    By 'better' do you mean code that does what you first posted above?

    Dim A As Integer = 3
    
    ... In button1 click event
           TextBox1.text = "Line Number = " & A.ToString & vbCrLf)
           TextBox1.Text = DataLines(I).Line_Data & vbCrLf
    
    
    ... In button_2 click
           TextBox2.text = "Line Number = " & (A + 1).ToString  & vbCrLf)
           TextBox2.Text =DataLines(A + 1).Line_Data & vbCrLf

    You haven't explained what LNum is or why you are using a loop.
    • Edited by Acamar Thursday, August 31, 2017 3:49 AM fmt
    • Marked as answer by mipakteh Thursday, August 31, 2017 4:25 AM
    Thursday, August 31, 2017 3:48 AM

All replies

  • If we find the "Line Number" and  and "Line_Data" in Click Button_1 then

    in Click Button_2 must show "Line Number + 1" and "Line_Data + 1".That mean Advance one Data.

    In the first button click you are creating a collection that has all the line data:
                DataLines.Add(Digits)

    So in the second button click you select the one you want.  That would be DataLines(0) to select the first, DataLines(1) to select the second, and so on.  There is no need for a loop, simply select the one you want using the index.

    Wednesday, August 30, 2017 9:48 AM
  • May be my explanation not clear Acamar,sorry.

    In Click_button_1;

      For I As Integer = 0 To DataLines.Count - 1
                If CDbl(I.ToString) = 2 Then
                    TextBox1.AppendText("Line Number = " & I.ToString & vbCrLf)
                    TextBox1.AppendText(DataLines(I).Line_Data & vbCrLf)
                    LNum.Add(CInt(I.ToString))
                    LData.Add(DataLines(I).Line_Data)
                End If
            Next

    The result in Textbox1 show;

    Line Number = 2

    9429,4624,8724,7704,3671,4701,4395,2168,3583,4832,7545,9270,9607,3931,2596,7327,0447,4277,9267,1724,1646,6181,0533

    and then I need  The result in Textbox2 show;

    Line Number = 3

    2719,8619,1121,8176,6265,6792,4738,7083,4193,3013,7757,2923,3087,1162,0485,9365,7385,1742,0097,2836,4814,7867,3382

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    
            For I As Integer = 0 To LNum.Count - 1
                Dim A As String = LNum(I).ToString
                TextBox2.AppendText("Line Number = " & A(I).ToString & vbCrLf & DataLines(I).Line_Data & vbCrLf)
            Next
    
    
        End Sub
    

    Wednesday, August 30, 2017 2:41 PM
  • May be my explanation not clear Acamar,sorry.

    That still doesn't make any sense.

    In the Button1Click event you already do this:

        Dim Digits As New myData With {.Line_Data = StringToCheck}
        DataLines.Add(Digits)
    So you now have a collection of myData objects.   You can then do whatever you want with any item in that collection.  For instance, in the Button1.Click event (after the loop ends) you could do
        TextBox1.Text = "Line Number = 2" & vbCrLf
        TextBox1.Text = DataLines(1).Line_Data & vbCrLf

    I don't know what LNum or LData are so I don't know what you are trying to do with that extra code.  Then in Button3.Click event you could do

        TextBox2.Text = "Line Number = 3" & vbCrLf
        TextBox2.Text = DataLines(2).Line_Data & vbCrLf
    You should spend more time in considering what it is that you are trying to do.  If you can't explain it to someone else then it's likely that you can't explain it to yourself.
    • Edited by Acamar Wednesday, August 30, 2017 9:42 PM sp
    Wednesday, August 30, 2017 9:41 PM
  • Thank Acamar,Yes sometimes I don't how to explain.When I thinking your suggestion.Bellow I try to illustrate;

    button_1 click;

            For I As Integer = 0 To DataLines.Count - 1
                If CDbl(I.ToString) = 3 Then
                    TextBox1.AppendText("Line Number = " & I.ToString & vbCrLf)
                    TextBox1.AppendText(DataLines(I).Line_Data & vbCrLf)
                    LNum.Add(CInt(I.ToString))
                End If
            Next
    

    button_2 click;

     For I As Integer = 0 To LNum.Count - 1
                Dim A As Integer = LNum(I) + 1
                TextBox2.AppendText("Line Number = " & A & vbCrLf)
                If LNum(I) + 1 = A Then
                    TextBox2.AppendText(DataLines(A).Line_Data & vbCrLf)
                End If
            Next

    If you have to suggest a better code.

    Thursday, August 31, 2017 3:36 AM
  • If you have to suggest a better code.

    By 'better' do you mean code that does what you first posted above?

    Dim A As Integer = 3
    
    ... In button1 click event
           TextBox1.text = "Line Number = " & A.ToString & vbCrLf)
           TextBox1.Text = DataLines(I).Line_Data & vbCrLf
    
    
    ... In button_2 click
           TextBox2.text = "Line Number = " & (A + 1).ToString  & vbCrLf)
           TextBox2.Text =DataLines(A + 1).Line_Data & vbCrLf

    You haven't explained what LNum is or why you are using a loop.
    • Edited by Acamar Thursday, August 31, 2017 3:49 AM fmt
    • Marked as answer by mipakteh Thursday, August 31, 2017 4:25 AM
    Thursday, August 31, 2017 3:48 AM
  • Thank Acamar code work.

    LNum keep the track for Line Number and using Loop because to find all of line number from text file line.

    Thursday, August 31, 2017 4:24 AM