Is there a list (of string) or equivalent in VB that I can configure to only hold x entries and then overwrite oldest

Darren Rose 496 Reputation points
2020-12-05T13:56:26.65+00:00

Is there a list (of string) or equivalent in VB that I can configure to only hold x entries and then overwrite oldest each time I add something new to it?

e.g. I can set it to hold say 25 items, and then if I add a 26th item it overwrites the first item and so on

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,578 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-12-07T15:55:59.697+00:00

    HI,
    you create your own list class like in following console demo. Add insert item at the top of list.

    Module Module64  
      Sub Main()  
        Try  
          Call (New Demo).Execute()  
        Catch ex As Exception  
          Console.WriteLine(ex.ToString)  
        End Try  
        Console.WriteLine("Continue enter key")  
        Console.ReadKey()  
      End Sub  
      
      Friend Class Demo  
        Friend Sub Execute()  
          Dim itemList As New MyList(Of String)(5)  
          For i = 1 To 7  
            itemList.Add($"Item {i}")  
          Next  
          For Each item In itemList  
            Console.WriteLine(item)  
          Next  
        End Sub  
      End Class  
      
      Friend Class MyList(Of T)  
        Inherits List(Of T)  
      
        Private _maxCount As Integer  
        Public Sub New(maxCount As Integer)  
          Me._maxCount = maxCount  
        End Sub  
      
        Friend Shadows Sub Add(item As T)  
          Me.Insert(0, item)  
          If Me.Count > Me._maxCount Then Me.RemoveAt(Me.Count - 1)  
        End Sub  
      End Class  
      
    End Module  
    

    Result:

    45730-x.png

    1 person found this answer helpful.
    0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Dewayne Basnett 1,116 Reputation points
    2020-12-07T20:09:42.083+00:00

    Circular Lists

    The Class

        ''' <summary>
        ''' a list with a limit
        ''' </summary>
        ''' <typeparam name="T">the type of data</typeparam>
        ''' <remarks></remarks>
        Public Class ListLimit(Of T)
            Private _max As Integer = 0
            Private _List As New List(Of T)
            Private _pos As Integer = 0
            Private _NewLast As Boolean = True
            ''' <summary>
            ''' list with max entires
            ''' </summary>
            ''' <param name="MaxEntries">maximum entries in list</param>
            ''' <param name="NewLast">True if new items added to end, False if new entries added to beginning</param>
            ''' <remarks></remarks>
            Public Sub New(MaxEntries As Integer,
                           Optional NewLast As Boolean = True)
                Me._max = MaxEntries
                Me._NewLast = NewLast
                If NewLast Then
                    Dim foo(MaxEntries - 1) As T
                    Me._List.AddRange(foo)
                End If
            End Sub
    
            Public Sub Add(item As T)
                If Me._NewLast Then
                    Me._List(Me._pos) = item
                    Me._pos += 1
                    If Me._pos = Me._max Then
                        Me._pos = 0
                    End If
                Else
                    Me._List.Insert(0, item)
                    If Me._List.Count = Me._max + 1 Then
                        Me._List.RemoveAt(Me._max)
                    End If
                End If
            End Sub
    
            Public Function TheList() As List(Of T)
                Return Me._List
            End Function
        End Class
    

    Test Code

            Debug.WriteLine("---------- Circular List ----------------")
            Debug.WriteLine("-------TEST1 step by step - keep 4 newest")
            Dim TEST1 As New ListLimit(Of String)(4, False)
            Dim N As DateTime = DateTime.Now
            Dim EndDT As DateTime = N.AddSeconds(11)
            Do
                TEST1.Add(N.ToString("HH:mm:ss"))
                Debug.WriteLine("Add " & N.ToString("HH:mm:ss") & " -------")
                For Each li As String In TEST1.TheList
                    If li IsNot Nothing Then Debug.WriteLine(li)
                Next
                Threading.Thread.Sleep(999)
                N = DateTime.Now
            Loop While N < EndDT
    
            Debug.WriteLine("")
            Debug.WriteLine("-------TEST2 step by step - keep 4 oldest")
            Dim TEST2 As New ListLimit(Of String)(4)
            N = DateTime.Now
            EndDT = N.AddSeconds(11)
            Do
                TEST2.Add(N.ToString("HH:mm:ss"))
                Debug.WriteLine("Add " & N.ToString("HH:mm:ss") & " -------")
                For Each li As String In TEST2.TheList
                    If li IsNot Nothing Then Debug.WriteLine(li)
                Next
                Threading.Thread.Sleep(999)
                N = DateTime.Now
            Loop While N < EndDT
    

    Results

    **---------- Circular List ----------------
    -------TEST1 step by step - keep 4 newest
    Add 14:06:02 -------
    14:06:02
    Add 14:06:03 -------
    14:06:03
    14:06:02
    Add 14:06:04 -------
    14:06:04
    14:06:03
    14:06:02
    Add 14:06:05 -------
    14:06:05
    14:06:04
    14:06:03
    14:06:02
    Add 14:06:06 -------
    14:06:06
    14:06:05
    14:06:04
    14:06:03
    Add 14:06:07 -------
    14:06:07
    14:06:06
    14:06:05
    14:06:04
    Add 14:06:08 -------
    14:06:08
    14:06:07
    14:06:06
    14:06:05
    Add 14:06:09 -------
    14:06:09
    14:06:08
    14:06:07
    14:06:06
    Add 14:06:10 -------
    14:06:10
    14:06:09
    14:06:08
    14:06:07
    Add 14:06:11 -------
    14:06:11
    14:06:10
    14:06:09
    14:06:08
    Add 14:06:12 -------
    14:06:12
    14:06:11
    14:06:10
    14:06:09

    -------TEST2 step by step - keep 4 oldest
    Add 14:06:13 -------
    14:06:13
    Add 14:06:14 -------
    14:06:13
    14:06:14
    Add 14:06:15 -------
    14:06:13
    14:06:14
    14:06:15
    Add 14:06:16 -------
    14:06:13
    14:06:14
    14:06:15
    14:06:16
    Add 14:06:17 -------
    14:06:17
    14:06:14
    14:06:15
    14:06:16
    Add 14:06:18 -------
    14:06:17
    14:06:18
    14:06:15
    14:06:16
    Add 14:06:19 -------
    14:06:17
    14:06:18
    14:06:19
    14:06:16
    Add 14:06:20 -------
    14:06:17
    14:06:18
    14:06:19
    14:06:20
    Add 14:06:21 -------
    14:06:21
    14:06:18
    14:06:19
    14:06:20
    Add 14:06:22 -------
    14:06:21
    14:06:22
    14:06:19
    14:06:20
    Add 14:06:23 -------
    14:06:21
    14:06:22
    14:06:23
    14:06:20**