none
List of T items RRS feed

  • Question

  • Hi All,

    I have problem FOR STATEMENT .bellow is code,

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private C_ As New List(Of String)
        Private F_ As New List(Of String)
        Private S_ As New List(Of String)
    
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            For i As Integer = 0 To C_.Count - 1
                ListBox1.Items.Add(C_(i) & "   " & F_(i))
            Next
    
            For i As Integer = 0 To C_.Count - 1
                TextBox1.AppendText(C_(i) & "   " & F_(i) & vbCrLf)
            Next
    
    
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            C_.Add("99")
            C_.Add("99")
            C_.Add("55")
            C_.Add("92")
            C_.Add("92")
            C_.Add("76")
            C_.Add("17")
            C_.Add("53")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("321")
            F_.Add("442")
            F_.Add("505")
            F_.Add("665")
            F_.Add("663")
            F_.Add("321")
    
    
        End Sub
    
        Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
    
            Dim tempList As New List(Of Integer)
    
            For Each s As String In F_
                If Integer.TryParse(s, New Integer) Then
                    If s.Length = 3 Then
                        If Not s(0) = s(1) AndAlso Not s(0) = s(2) AndAlso Not s(1) = s(2) Then
                            tempList.Add(CInt(s))
                        End If
                    Else
                        tempList.Add(CInt(s))
                    End If
                End If
            Next
    
            For j As Integer = 0 To tempList.Count - 1
    
                TextBox4.AppendText(CStr(tempList(j) & "  " & C_(j) & vbCrLf))
            Next
    

    and  get the result like that;

    the Result Expected is;

    Saturday, March 25, 2017 3:34 AM

Answers

  • I have problem FOR STATEMENT .bellow is code,

    There is no reason to expect that code to produce the result you have shown.

    This is the code:
                TextBox4.AppendText(CStr(tempList(j) & "  " & C_(j) & vbCrLf))

    The first four items in tempList() will be
    203
    205
    321
    321

    and the first four items in C_() are:

    99
    99
    55
    92

    Which is what you are getting.  You choice of sample data is partially disguising the problem - you have too many similar values.

    If you wanted the value associated with each item in templist then you have to create a second list templist2 and add the C_ item to templist 2 at the same time as the add the F_ item to templist1.



    • Edited by Acamar Saturday, March 25, 2017 5:23 AM fmt
    • Marked as answer by mipakteh Saturday, March 25, 2017 7:28 AM
    Saturday, March 25, 2017 5:17 AM
  • Hi

    This is as close as I can get to understanding your question. Some code has not been included because I don't understand it. This does produce your expected result.

    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Private C_ As New List(Of String)
        Private F_ As New List(Of String)
        Private S_ As New List(Of String)
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            C_.Add("99")
            C_.Add("99")
            C_.Add("55")
            C_.Add("92")
            C_.Add("92")
            C_.Add("76")
            C_.Add("17")
            C_.Add("53")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("321")
            F_.Add("442")
            F_.Add("505")
            F_.Add("665")
            F_.Add("663")
            F_.Add("321")
        End Sub
        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ind As Integer = 0
            For Each s As String In F_
                If Integer.TryParse(s, New Integer) Then
                    If s.Length = 3 Then
                        If Not (s(0) = s(1) Or s(0) = s(2) Or s(1) = s(2)) Then
                            TextBox4.AppendText(s & "  " & C_(ind) & vbCrLf)
                        End If
                    End If
                End If
                ind += 1
            Next
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by mipakteh Saturday, March 25, 2017 7:28 AM
    Saturday, March 25, 2017 5:55 AM

All replies

  • Hi

    I can't get the same result as you get in the first pair of lists.Would you elaborate on what you want to happen.


    Regards Les, Livingston, Scotland

    Saturday, March 25, 2017 4:48 AM
  • Hi leshay,

    We have 2 list of T, C_ and F_.In F_ list we not need any pair of number,Example 101,442,505,665,663 so we remove that number and leave the other number in List of Templist-( 203,205,321,321.)

    If list Templist contain 203,205,321,321 then List C_ item should be contain 99,55,92,31.

    Thank.

    Saturday, March 25, 2017 5:05 AM
  • I have problem FOR STATEMENT .bellow is code,

    There is no reason to expect that code to produce the result you have shown.

    This is the code:
                TextBox4.AppendText(CStr(tempList(j) & "  " & C_(j) & vbCrLf))

    The first four items in tempList() will be
    203
    205
    321
    321

    and the first four items in C_() are:

    99
    99
    55
    92

    Which is what you are getting.  You choice of sample data is partially disguising the problem - you have too many similar values.

    If you wanted the value associated with each item in templist then you have to create a second list templist2 and add the C_ item to templist 2 at the same time as the add the F_ item to templist1.



    • Edited by Acamar Saturday, March 25, 2017 5:23 AM fmt
    • Marked as answer by mipakteh Saturday, March 25, 2017 7:28 AM
    Saturday, March 25, 2017 5:17 AM
  • Hi

    This is as close as I can get to understanding your question. Some code has not been included because I don't understand it. This does produce your expected result.

    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Private C_ As New List(Of String)
        Private F_ As New List(Of String)
        Private S_ As New List(Of String)
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            C_.Add("99")
            C_.Add("99")
            C_.Add("55")
            C_.Add("92")
            C_.Add("92")
            C_.Add("76")
            C_.Add("17")
            C_.Add("53")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("321")
            F_.Add("442")
            F_.Add("505")
            F_.Add("665")
            F_.Add("663")
            F_.Add("321")
        End Sub
        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ind As Integer = 0
            For Each s As String In F_
                If Integer.TryParse(s, New Integer) Then
                    If s.Length = 3 Then
                        If Not (s(0) = s(1) Or s(0) = s(2) Or s(1) = s(2)) Then
                            TextBox4.AppendText(s & "  " & C_(ind) & vbCrLf)
                        End If
                    End If
                End If
                ind += 1
            Next
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by mipakteh Saturday, March 25, 2017 7:28 AM
    Saturday, March 25, 2017 5:55 AM
  • thank you very much, code working
    • Marked as answer by mipakteh Saturday, March 25, 2017 7:28 AM
    • Unmarked as answer by mipakteh Saturday, March 25, 2017 7:28 AM
    Saturday, March 25, 2017 7:27 AM
  • thank Acamar;

        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim tempList As New List(Of Integer)
            Dim ListItems As New List(Of String)
    
            Dim ind As Integer = 0
            For Each s As String In F_
                If Integer.TryParse(s, New Integer) Then
                    If s.Length = 3 Then
                        If Not (s(0) = s(1) Or s(0) = s(2) Or s(1) = s(2)) Then
                            tempList.Add(CInt(s))
    
                            TextBox1.AppendText(s & "  " & C_(ind) & vbCrLf)
                            ListItems.Add(C_(ind))
    
                        End If
                    End If
                End If
    
    
                ind += 1
            Next
            For j As Integer = 0 To tempList.Count - 1
                If tempList(j) = tempList.Max Then TextBox2.AppendText(tempList.Max & " " & ListItems(j) & vbCrLf)
            Next
    
    
        End Sub
    

    Saturday, March 25, 2017 7:28 AM
  • thank Acamar;

    You make things very difficult for yourself by not using custom classes.  This is how this type of problem would be coded using a class for the data.

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private FC_ As New List(Of Pair)
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            For i As Integer = 0 To FC_.Count - 1
                ListBox1.Items.Add(FC_(i).ToStringCF)
            Next
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            FC_.Add(New Pair("101", "99"))
            FC_.Add(New Pair("203", "99"))
            FC_.Add(New Pair("205", "55"))
            FC_.Add(New Pair("321", "92"))
            FC_.Add(New Pair("442", "92"))
            FC_.Add(New Pair("505", "76"))
            FC_.Add(New Pair("665", "17"))
            FC_.Add(New Pair("663", "53"))
            FC_.Add(New Pair("321", "31"))
        End Sub
    
        Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
            Dim tempList As New List(Of Pair)
            For I As Integer = 0 To FC_.Count - 1
                Dim s As String = FC_(I).F_
                If Integer.TryParse(s, New Integer) Then
                    If s.Length = 3 Then
                        If Not s(0) = s(1) AndAlso Not s(0) = s(2) AndAlso Not s(1) = s(2) Then
                            tempList.Add(FC_(I))
                        End If
                    Else
                        tempList.Add(FC_(I))
                    End If
                End If
            Next
            For j As Integer = 0 To tempList.Count - 1
                TextBox4.AppendText(tempList(j).ToStringFC & vbCrLf)
            Next
        End Sub
    
    End Class
    
    Public Class Pair
        Public Property F_ As String
        Public Property C_ As String
    
        Public Sub New(F As String, C As String)
            _F_ = F
            _C_ = C
        End Sub
    
        Public Function ToStringFC() As String
            Return _F_ & " " & _C_
        End Function
    
        Public Function ToStringCF() As String
            Return _C_ & " " & _F_
        End Function
    
    End Class

    Saturday, March 25, 2017 7:49 AM
  • Thank you very much Acamar;

    If I want to show maxima number F_ in Templist ,How?

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim tempList As New List(Of Pair)
            For I As Integer = 0 To FC_.Count - 1
                Dim s As String = FC_(I).F_
                If Integer.TryParse(s, New Integer) Then
                    If s.Length = 3 Then
                        If Not s(0) = s(1) AndAlso Not s(0) = s(2) AndAlso Not s(1) = s(2) Then
                            tempList.Add(FC_(I))
                        End If
                    Else
                        tempList.Add(FC_(I))
                    End If
                End If
            Next
            For j As Integer = 0 To tempList.Count - 1
                TextBox1.AppendText(tempList(j).ToStringFC & vbCrLf)
                ' How to show the Max of F_ in Templist.
                If tempList(j).F_ = tempList(j).F_.Max Then TextBox2.AppendText(tempList(j).F_.Max & "   " & tempList(j).C_ & vbCrLf)
    
            Next
        End Sub
    

    Sunday, March 26, 2017 4:53 AM
  • If I want to show maxima number F_ in Templist ,How?

    The code you use to find the maximum value is the same for any list of values - just change the variable that you are testing:

            Dim Max As Integer = Integer.MinValue
            For j As Integer = 0 To tempList.Count - 1
                TextBox4.AppendText(tempList(j).ToStringFC & vbCrLf)
                If CInt(tempList(j).F_) > Max Then Max = CInt(tempList(j).F_)
            Next
            Label1.text = Max.ToString

    Sunday, March 26, 2017 6:42 AM
  • Acamar;

    The result only show one from the Max number.

    In Templist

    203 99
    205 55
    321 92
    321 31

    In list F_ we have two Max number

    321 92

    321 31

    How to show that ?.

    Thank Acamar for your help

    Sunday, March 26, 2017 8:38 AM
  • The result only show one from the Max number.

    You asked to show the maximum number F_, and that's what the above code does.  If you want to show the pair instances which have a F_ value equal to the maximum, then you will need another loop which iterates over the temp list and shows any pair (using StringToFC, by your example) where the F_ value equals the maximum calculated using that code. 

    Sunday, March 26, 2017 9:17 AM