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,581 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-07T14:53:24.943+00:00

    Answer moved to later in this tread.

    0 comments No comments

  2. Darren Rose 496 Reputation points
    2020-12-07T14:58:49.11+00:00

    @Dewayne Basnett

    Thanks for that

    As an amendment to my original question, is the below possible:-

    Basically the list (of string) will hold historical related information - so would be handy if the list could be reversed e.g. newest item gets added to top (rather than bottom) of the list, and oldest gets removed from the bottom of the list (rather than top), so when I then output it to wherever it would appear in correct order - hope this makes sense


  3. Darren Rose 496 Reputation points
    2020-12-07T15:34:26.833+00:00

    I thought that was quite clear from my post... but reversing the current order, as at the moment anything new you add goes to bottom of list and the top oldest one is removed, wheras ideally (but not essential, was just asking if possible) I want to add new to TOP of list and removed the old one from BOTTOM

    So if we added 1, 2, 3, 4, 5 then it would be stored as 5, 4, 3, 2,1 as oldest at bottom, newest at top


  4. Darren Rose 496 Reputation points
    2020-12-07T16:03:49.197+00:00

    anonymous user-3316

    Perfect - thanks

    0 comments No comments