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,727 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,321 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. Anonymous
    2020-12-05T14:16:20.677+00:00

    Hi
    Try this example and see if it works as you want. Set the MaxItems variable to whatever you want.

    ' Form1 with ListBox1
    ' and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim MaxItems As Integer = 6
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static c As Integer = 1
        With ListBox1
          .Items.Add("Item" & c.ToString)
          c += 1
          If .Items.Count > MaxItems Then
            .Items.RemoveAt(0)
          End If
        End With
      End Sub
    End Class
    
    1 person found this answer helpful.
    0 comments No comments

  2. Peter Fleischer (former MVP) 19,321 Reputation points
    2020-12-05T16:31:09.68+00:00

    Hi,
    you create your own list class based an FIFO queue like in following console demo:

    Module Module1
      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()
    ' instantiate List with max count 5 '
          Dim itemList As New MyList(Of String)(5)
    ' load list with 7 items '
          For i = 1 To 7
            itemList.Add($"Item {i}")
          Next
    ' display result - only 5 items '
          For Each item In itemList
            Console.WriteLine(item)
          Next
        End Sub
      End Class
    
      Friend Class MyList(Of T)
        Inherits Queue(Of T)
    
        Private _maxCount As Integer
        Public Sub New(maxCount As Integer)
          Me._maxCount = maxCount
        End Sub
    
        Friend Sub Add(item As T)
          Me.Enqueue(item)
          If Me.Count > Me._maxCount Then Me.Dequeue()
        End Sub
      End Class
    
    End Module
    
    1 person found this answer helpful.

  3. Darren Rose 496 Reputation points
    2020-12-05T14:26:19.89+00:00

    Hi LesHay,

    Thank you, that solution led me down the right path to do same for a List (Of String)

    Public Class Form1
    
        Dim MaxItems As Integer = 6
        Dim mylist As New List(Of String)
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            With mylist
                .Add("Item" & c.ToString)
                c += 1
                If .Count > MaxItems Then
                    .RemoveAt(0)
                End If
            End With
    
            ' output to listbox to confirm working
            ListBox1.Items.Clear()
    
            For Each item In mylist
                ListBox1.Items.Add(item)
            Next
    
        End Sub
    
    End Class
    
    0 comments No comments

  4. Karen Payne MVP 35,421 Reputation points
    2020-12-06T12:52:10.477+00:00

    Hello @Darren Rose

    Another option is using a class which inherits Stack

    Public Class FixedSizeStack  
        Inherits Stack  
      
        Private MaxNumber As Integer  
        Public Sub New(Limit As Integer)  
            MaxNumber = Limit  
        End Sub  
      
        Public Overrides Sub Push(obj As Object)  
            If Count < MaxNumber Then  
                MyBase.Push(obj)  
            End If  
        End Sub  
    End Class  
    

    Full example using integer then string and kept it simple.

    Module Module1  
      
        Sub Main()  
            Dim originalColor = Console.ForegroundColor  
      
            Dim StackData1 As New FixedSizeStack(5)  
      
            Console.ForegroundColor = ConsoleColor.White  
            Console.WriteLine("Adding")  
            Console.ForegroundColor = originalColor  
            For index As Integer = 0 To 9  
                StackData1.Push(index + 1)  
            Next index  
      
            Console.WriteLine()  
      
      
            Console.ForegroundColor = ConsoleColor.White  
            Console.WriteLine("Items in StackData")  
            Console.ForegroundColor = originalColor  
      
            Dim results = StackData1.OfType(Of Integer)().OrderBy(Function(x) x).ToList()  
            For Each result As Integer In results  
                Console.WriteLine(result)  
            Next  
      
            Dim StackData2 As New FixedSizeStack(3)  
            StackData2.Push("A")  
            StackData2.Push("B")  
            StackData2.Push("C")  
            StackData2.Push("D")  
            StackData2.Push("E")  
            StackData2.Push("F")  
            StackData2.Push("G")  
      
      
            Console.WriteLine()  
            Console.ForegroundColor = ConsoleColor.White  
            Console.WriteLine("Items in StackData2")  
            Console.ForegroundColor = originalColor  
      
            Dim results1 = StackData2.OfType(Of String).Reverse().ToList()  
      
            For Each result As String In results1  
                Console.WriteLine(result)  
            Next  
      
            Console.ReadLine()  
      
        End Sub  
      
    End Module  
    Public Class FixedSizeStack  
        Inherits Stack  
      
        Private MaxNumber As Integer  
        Public Sub New(Limit As Integer)  
            MaxNumber = Limit  
        End Sub  
      
        Public Overrides Sub Push(obj As Object)  
            If Count < MaxNumber Then  
                MyBase.Push(obj)  
            End If  
        End Sub  
    End Class  
    

    45410-b1.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.