none
Max in Class RRS feed

  • Question

  • Hi All,

    My question is How;

    By Click Button_3 then the Result show like this.Searching for maxima Number in List C_.

    A  101    99
    B  203    99

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    Imports System.Windows.Forms
    
    Public Class Form1
    
        Private A_ As New List(Of String)
        Private B_ As New List(Of String)
        Private C_ As New List(Of String)
    
        Private Items As New List(Of ItemInfo)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Items.Add(New ItemInfo("A", "101", "99"))
            Items.Add(New ItemInfo("B", "203", "99"))
            Items.Add(New ItemInfo("C", "665", "78"))
            Items.Add(New ItemInfo("D", "205", "55"))
            Items.Add(New ItemInfo("E", "321", "92"))
            Items.Add(New ItemInfo("F", "442", "92"))
            Items.Add(New ItemInfo("G", "505", "76"))
            Items.Add(New ItemInfo("H", "665", "17"))
            Items.Add(New ItemInfo("I", "663", "53"))
            Items.Add(New ItemInfo("J", "321", "31"))
    
            For I As Integer = 0 To Items.Count - 1
                TextBox1.AppendText(Items(I).ToString & vbCrLf)
            Next
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            For J As Integer = 0 To Items.Count - 1
                TextBox2.AppendText(Items(J).A_.ToString & vbCrLf)
            Next
            For J As Integer = 0 To Items.Count - 1
                TextBox3.AppendText(Items(J).B_.ToString & vbCrLf)
            Next
            For J As Integer = 0 To Items.Count - 1
                TextBox4.AppendText(Items(J).C_.ToString & vbCrLf)
            Next
    
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    
            For J As Integer = 0 To Items.Count - 1
                If CDbl(Items(J).C_) = 17 Then
                    TextBox5.AppendText(Items(J).ToString & vbCrLf)
                End If
            Next
    
        End Sub
    
    
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    
            For J As Integer = 0 To Items.Count - 1
                If Items(J).C_ = Items(J).C_.Max Then
                    TextBox7.AppendText(Items(J).C_.Max & vbCrLf)
                End If
            Next
    
    
        End Sub
    
    End Class
    
    Public Class ItemInfo
        Public Property A_ As String
        Public Property B_ As String
        Public Property C_ As String
        Public Sub New(a As String, b As String, c As String)
            A_ = a
            B_ = b
            C_ = c
        End Sub
        Public Overrides Function ToString() As String
            Return A_ & "  " & B_ & "    " & C_
        End Function
    End Class

    Thank.


    • Edited by mipakteh Sunday, May 20, 2018 7:48 AM Add
    Sunday, May 20, 2018 7:47 AM

Answers

  • By Click Button_3 then the Result show like this.Searching for maxima Number in List C_.

    A  101    99
    B  203    99

    You can't find which items are the maximum until you know what the maximum is.  So there are two loops required.

    Dim Max As Integer = 0        
    For J As Integer = 0 To Items.Count - 1
        If Items(J).C_ > Max then Max = Items(J).C_
    Next
    For J As Integer = 0 To Items.Count - 1
        If Items(J).C_ = Max Then
            TextBox7.AppendText(Items(J).ToString & vbCrLf)
        End If
    Next

    • Marked as answer by mipakteh Sunday, May 20, 2018 11:19 AM
    Sunday, May 20, 2018 9:07 AM

All replies

  • It seems to me that you have a lot of unnecessary code.

    Why are you not using Linq to query the List(Of T)?

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/linq/introduction-to-linq

    Why are you not using a Linq Aggregate like Max() and return the results?

    https://www.tutlane.com/tutorial/linq/linq-max-function-to-get-maximum-value-from-list

    https://code.msdn.microsoft.com/LINQ-Sample-Queries-13a42a54

    Sunday, May 20, 2018 8:29 AM
  • By Click Button_3 then the Result show like this.Searching for maxima Number in List C_.

    A  101    99
    B  203    99

    You can't find which items are the maximum until you know what the maximum is.  So there are two loops required.

    Dim Max As Integer = 0        
    For J As Integer = 0 To Items.Count - 1
        If Items(J).C_ > Max then Max = Items(J).C_
    Next
    For J As Integer = 0 To Items.Count - 1
        If Items(J).C_ = Max Then
            TextBox7.AppendText(Items(J).ToString & vbCrLf)
        End If
    Next

    • Marked as answer by mipakteh Sunday, May 20, 2018 11:19 AM
    Sunday, May 20, 2018 9:07 AM
  • thank for advice.
    Sunday, May 20, 2018 11:19 AM
  • Thank Acamar;

    Sunday, May 20, 2018 11:19 AM