none
Class Object RRS feed

  • Question

  • Hi All,

    My question is;

    How to count the maxItem and minItem in class as object.

    What I trying , add this code to,

      Dim maxcount As Integer = 0
    
                For I As Integer = 0 To _Items.Count - 1
                    If _Items(I).Value = max Then
                        maxcount += 1
                    End If
                Next

    into ;

     Public Property Items As List(Of myItem)
            Get
                Return _Items
            End Get
            Set(ByVal Value As List(Of myItem))
                _Items = Value
                Dim min As Integer = Integer.MaxValue
                Dim max As Integer = Integer.MinValue
    
                For Each ITM As myItem In _Items
                    If ITM.Value < min Then
                        min = ITM.Value
                    End If
                    If ITM.Value > max Then
                        max = ITM.Value
                    End If
                Next
    
                Dim maxcount As Integer = 0
    
                For I As Integer = 0 To _Items.Count - 1
                    If _Items(I).Value = max Then
                        maxcount += 1
                    End If
                Next
    
                _maxcount = maxcount
                _maxvalue = max
                _minvalue = min
                _maxitems = New List(Of myItem)
                _minitems = New List(Of myItem)
                _MCount = New List(Of myItem)
    
                For Each ITM As myItem In _Items
                    If ITM.Value = MinValue Then
                        _minitems.Add(ITM)
                    End If
                    If ITM.Value = MaxValue Then
                        _maxitems.Add(ITM)
                    End If
                    If ITM.Value = maxcount Then
                        _MCount.Add(ITM)
                    End If
    
                Next
            End Set
        End Property

    then add Function class;

     Public Function maxcountToString() As String
            Return maxcount & " maxcount"
        End Function
    

    But not succesed.

    The whole code as bellow.

    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()
            TextBox2.Clear()
    
            Dim sr As New System.IO.StreamReader("C:\Users\family\Documents\B.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
    
                For Each ITM As myItem In MyItems
                    TextBox1.AppendText(ITM.ToString & " ," & vbCrLf)
                Next
    
                Dim Result_ As New Dictionary(Of String, Integer)
    
                For Each ITM As myItem In MyItems
                    If Result_.Keys.Contains(CStr(ITM.Value)) Then
                        Result_(CStr(ITM.Value)) += 1
                    Else
                        Result_.Add(CStr(ITM.Value), 1)
                    End If
                Next
    
    
                Dim thisData As New myData With {.Line = StringToCheck, .Pairs = Result_, _
                                                 .PairCount = ShowList(Result_), _
                                                     .Items = MyItems}
                DataLines.Add(thisData)
                TextBox1.AppendText(vbCrLf)
    
            Loop
    
            For Each DTA As myData In DataLines
                If DTA.PairCount = 2 Then
                    TextBox2.AppendText(DTA.PairsToString & vbCrLf)
                    TextBox2.AppendText(DTA.MaxList & vbCrLf)
                    TextBox2.AppendText(DTA.MinList & vbCrLf)
                    TextBox2.AppendText("Pair Count = " & DTA.PairCount.ToString & vbCrLf)
                    TextBox2.AppendText(DTA.LineToString & vbCrLf)
                    TextBox2.AppendText(DTA.maxcountToString & vbCrLf)
    
                End If
            Next
    
        End Sub
    
        Public Function ShowList(ItemList As Dictionary(Of String, Integer)) As Integer
            Dim PairCount As Integer = 0
            For Each KV As KeyValuePair(Of String, Integer) In ItemList
                TextBox1.AppendText(KV.Key & "=" & KV.Value.ToString & ", ")
                If KV.Value > 1 Then
                    PairCount += 1
                End If
            Next
            TextBox1.AppendText(vbCrLf)
            TextBox1.AppendText("Pair Count = " & PairCount & "   " & vbCrLf)
            Return PairCount
    
        End Function
    
    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
    
        Private _Items As List(Of myItem)
        Public Property Items As List(Of myItem)
            Get
                Return _Items
            End Get
            Set(ByVal Value As List(Of myItem))
                _Items = Value
                Dim min As Integer = Integer.MaxValue
                Dim max As Integer = Integer.MinValue
    
                For Each ITM As myItem In _Items
                    If ITM.Value < min Then
                        min = ITM.Value
                    End If
                    If ITM.Value > max Then
                        max = ITM.Value
                    End If
                Next
    
                Dim maxcount As Integer = 0
    
                For I As Integer = 0 To _Items.Count - 1
                    If _Items(I).Value = max Then
                        maxcount += 1
                    End If
                Next
    
                _maxcount = maxcount
                _maxvalue = max
                _minvalue = min
                _maxitems = New List(Of myItem)
                _minitems = New List(Of myItem)
                _MCount = New List(Of myItem)
    
                For Each ITM As myItem In _Items
                    If ITM.Value = MinValue Then
                        _minitems.Add(ITM)
                    End If
                    If ITM.Value = MaxValue Then
                        _maxitems.Add(ITM)
                    End If
                    If ITM.Value = maxcount Then
                        _MCount.Add(ITM)
                    End If
    
                Next
            End Set
        End Property
        Private _maxvalue As Integer
        Private _minvalue As Integer
        Private _maxcount As Integer
    
        Public ReadOnly Property MaxValue As Integer
            Get
                Return _maxvalue
            End Get
        End Property
        Public ReadOnly Property MinValue As Integer
            Get
                Return _minvalue
            End Get
        End Property
        Public ReadOnly Property maxcount As Integer
            Get
                Return _maxcount
            End Get
        End Property
    
        Public Line As String
        Public Pairs As Dictionary(Of String, Integer)
        Public PairCount As Integer
        Private _minitems As List(Of myItem)
        Private _maxitems As List(Of myItem)
        Private _MCount As List(Of myItem)
    
        Public ReadOnly Property MaxItems As List(Of myItem)
            Get
                Return _maxitems
            End Get
        End Property
        Public ReadOnly Property MinItems As List(Of myItem)
            Get
                Return _minitems
            End Get
        End Property
        Public ReadOnly Property MCount As List(Of myItem)
            Get
                Return _MCount
            End Get
        End Property
    
    
        Public Function PairsToString() As String
            Dim S As String = ""
            For Each thisItem As myItem In Items
                S &= thisItem.ToString & vbCrLf
            Next
            For Each KVP As KeyValuePair(Of String, Integer) In Pairs
                S &= KVP.Key & "=" & KVP.Value.ToString & ", "
            Next
            Return S
        End Function
        Public Function MaxList() As String
            Dim S As String = "Max of Digits "
            For Each ITM As myItem In _maxitems
                S &= ITM.Index.ToString & " : " & ITM.Value.ToString & " "
            Next
            Return S
        End Function
    
        Public Function MinList() As String
            Dim S As String = "Min of Digits "
            For Each ITM As myItem In _minitems
                S &= ITM.Index.ToString & " : " & ITM.Value.ToString & " "
            Next
            Return S
        End Function
    
        Public Function LineToString() As String
            Return Line & " Line Of Data"
        End Function
        Public Function maxcountToString() As String
            Return maxcount & " maxcount"
        End Function
    
    
    End Class
    
    

    thank.

    Saturday, August 26, 2017 1:18 PM

Answers

  • How to count the maxItem and minItem in class as object.

    You haven't provided a sample of your data or the result you expect to get, so the code below is just a guess.

        Public ReadOnly Property MaxCountToString As String
            Get
                Return _maxitems.Count.ToString & " maxcount"
            End Get
        End Property
        Public ReadOnly Property MinCountToString As String
            Get
                Return _minitems.Count.ToString & " mincount"
            End Get
        End Property
    However, as MaxItems and MinItems are already exposed as properties of the class, there seems little point in defining a new property that simply returns a formatted count for the lists.
    • Marked as answer by mipakteh Sunday, August 27, 2017 12:01 AM
    Saturday, August 26, 2017 11:11 PM

All replies

  • How to count the maxItem and minItem in class as object.

    You haven't provided a sample of your data or the result you expect to get, so the code below is just a guess.

        Public ReadOnly Property MaxCountToString As String
            Get
                Return _maxitems.Count.ToString & " maxcount"
            End Get
        End Property
        Public ReadOnly Property MinCountToString As String
            Get
                Return _minitems.Count.ToString & " mincount"
            End Get
        End Property
    However, as MaxItems and MinItems are already exposed as properties of the class, there seems little point in defining a new property that simply returns a formatted count for the lists.
    • Marked as answer by mipakteh Sunday, August 27, 2017 12:01 AM
    Saturday, August 26, 2017 11:11 PM
  • yes,Acamar. A liitle bit confusing.

    Thank.

    Sunday, August 27, 2017 12:01 AM