Results 1 to 7 of 7

Thread: Visual Basic .NET - Which Collection Should I Use?

Threaded View

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Visual Basic .NET - Which Collection Should I Use?

    Common Theme

    Which collection should I use to store my values? This guide will help you choose the appropriate VB.NET collection based on your specific needs.

    Solution

    The recommendations below provide best practices to help you select the right collection for your data. These are suggestions, and in some cases, you may need to modify based on your specific scenario.

    Array - Fixed Size Collection

    Question: I have a fixed set of values, and I know the exact number. No more values will be added or removed. What collection should I use?
    Answer: Use an Array. Arrays are fixed-size collections that are highly efficient when you don’t need to modify the number of elements.
    Example:
    Code:
    Dim initializedArray() As Integer = {1, 2, 3}
    Dim fixedArray(2) As Integer
    fixedArray(0) = 1
    fixedArray(1) = 2
    fixedArray(2) = 3
    Documentation: https://learn.microsoft.com/en-us/do...atures/arrays/

    List(Of T) - Flexible, Indexed Collection

    Question: I need to add or remove values, and I want the flexibility to do this at any position within the collection. What collection should I use?
    Answer: Use a List(Of T). Lists allow dynamic resizing and let you insert or remove items at any index.
    Example:
    Code:
    Dim values As New List(Of Integer)
    values.Add(1)
    values.AddRange({1, 2, 3})
    values.RemoveAt(1)
    values.Insert(2, 88)
    Documentation: https://learn.microsoft.com/en-us/do...generic.list-1

    Queue(Of T) - First-In-First-Out Collection

    Question: I need to add values to the end of the collection and remove values from the start, following a "first-in, first-out" rule. What collection should I use?
    Answer: Use a Queue(Of T). Queues work on a FIFO (first-in-first-out) principle, allowing additions at the end and removals from the beginning.
    Example:
    Code:
    Dim values As New Queue(Of Integer)({1, 2, 3})
    values.Dequeue()
    values.Enqueue(4)
    values.Enqueue(5)
    Documentation: https://learn.microsoft.com/en-us/do...eneric.queue-1

    Stack(Of T) - Last-In-First-Out Collection

    Question: I need to add values to the end of the collection and remove values from the end, following a "last-in, first-out" rule. What collection should I use?
    Answer: Use a Stack(Of T). Stacks follow a LIFO (last-in-first-out) model, where the last item added is the first one removed.
    Example:
    Code:
    Dim values As New Stack(Of Integer)({1, 2, 3})
    values.Pop()
    values.Push(4)
    Documentation: https://learn.microsoft.com/en-us/do...eneric.stack-1

    HashSet(Of T) - Storing Unique Values

    Question: I have a collection of values, and I need to ensure that every value is unique. What collection should I use?
    Answer: Use a HashSet(Of T). A HashSet(Of T) is ideal when you need to store a unique set of elements. It does not allow duplicate values, and the order of elements is not guaranteed.
    Example:
    Code:
    Dim uniqueValues As New HashSet(Of Integer)
    uniqueValues.Add(1)
    uniqueValues.Add(2)
    uniqueValues.Add(2) 'This will not be added again
    uniqueValues.Add(3)
    Documentation: Documentation: https://learn.microsoft.com/en-us/do...eric.hashset-1

    Dictionary(Of TKey, TValue) - Key/Value Pairs with Unique Keys

    Question: I need to store pairs of values where one part of the pair must be unique. What collection should I use?
    Answer: Use a Dictionary(Of TKey, TValue). Dictionaries store key/value pairs where the key must be unique.
    Example:
    Code:
    Dim values As New Dictionary(Of Integer, String)
    values.Add(1, "a")
    values.Add(2, "b")
    values.Add(3, "c")
    Documentation: https://learn.microsoft.com/en-us/do...c.dictionary-2

    Custom Class for Key/Value Pairs without Unique Keys

    Question: I need to store pairs of values, but neither value needs to be unique. What collection should I use?
    Answer: Create a custom class to store key/value pairs, and use an array, list, queue, or stack depending on how you need to access the collection. For example, if you want flexibility to add or remove items at any position, use a List(Of T).

    Array with ReDim Preserve - Dynamic Sizing without List(Of T)

    Question: I need to add or remove values at any position within the collection, but I'm targeting a .NET framework version that does not support List(Of T). What should I use?
    Answer: Use an Array and employ the ReDim Preserve operation to resize the array while maintaining its existing values.
    Example:
    Code:
    Dim initializedArray() As Integer = {1, 2, 3, 4, 5}
    ReDim Preserve initializedArray(initializedArray.Length - 1)
    Documentation: https://learn.microsoft.com/en-us/do...edim-statement
    Last edited by dday9; Sep 27th, 2024 at 08:39 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width