Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a little understanding of List(of), Dictionary and Arrays, but with all the alternatives, I'm struggling to understand the best for my application. I have a number objects, not more than a couple of hundred, each has a name, a dimension (height x width), a type (one of five) and a colour. I need to be able to temporarily store them, and retrieve just one of them based on any or all of the values. Please could you advise on the best choice and perhaps with a simple example? I haven't added code I've tried as Dictionary didn't work, no key, List(of) didn't allow me different types and arrays seem like such an overkill. Many thanks in advance.

What I have tried:

Public Class Products
    Public Property Name As String
    Public Property Width As Integer
    Public Property Height As Integer
    Public Property Type As String
    Public Property Colour_Class As String
    Public Property Bool_Class As Boolean

    'Temporay values that I can access from another class
    Public Shared Name_Temp As String
    Public Shared Width_Temp As Integer
    Public Shared Height_Temp As Integer
    Public Shared Type_Temp As String
    Public Shared Color_Class_Temp As String
    Public Shared Bool_Class_Temp As Boolean

    Dim New_Products As New List(Of Products)

    Public Sub DoStuff()

        'Add individual Products to New_Products
        New_Products.Add(New Products With {.Name = "John", .Width = 222, .Height = 111, .Type = "metal", .Colour_Class = "Yellow", .Bool_Class = True})
        New_Products.Add(New Products With {.Name = "Fred", .Width = 100, .Height = 654, .Type = "plastic", .Colour_Class = "green", .Bool_Class = False})
        New_Products.Add(New Products With {.Name = "Harry", .Width = 555, .Height = 123, .Type = "wood", .Colour_Class = "blue", .Bool_Class = True})
        New_Products.Add(New Products With {.Name = "Jill", .Width = 333, .Height = 200, .Type = "cotton", .Colour_Class = "red", .Bool_Class = False})


        'Get individual values from product named John
        For Each Product In New_Products
            With Product
                If Product.Name = "John" Then
                    Name_Temp = .Name
                    Width_Temp = .Width.ToString
                    Type_Temp = .Type
                End If
            End With
        Next

        'Remove product where width = 100
        For i As Integer = 1 To New_Products.Count - 1
            If New_Products(i).Width = 100 Then
                New_Products.RemoveAt(1)
            End If
        Next i

    End Sub

End Class
Posted
Updated 31-Aug-23 1:31am
v2

Dictionaries are useful if you have keyed data. Each key needs to be a unique value.

Arrays are typically used for static collections, ie: fixed number of products. They can be resized but there is a cost involved.

Lists are used for dynamic collections that can easily have items added or removed as required.

There are other collection types however these are the most common. For more information, see: Commonly Used Collection Types | Microsoft Learn[^] and Collections - Visual Basic | Microsoft Learn[^]
 
Share this answer
 
v2
Create a class which has properties for each of the elements you need to store together: Name (String), Size (Size - which has Height and Width properties), type (Enum), and Colour (Color).
Then you can use a dictionary (Key is Name, Value is the object itself), an Array (if it's a fixed number of items that will never change*, or a List if the number can be increased or decreased.

* As Graeme has said, arrays can be resized, but there is a cost - and it can get pretty big. See here if you want to know why: List<T> - Is it really as efficient as you probably think?[^]
 
Share this answer
 
Comments
Member 15070471 31-Aug-23 7:43am    
Thank you for your suggestion of creating a class for the list of different data types. I've added the code above which I think is correct, just trying out the different methods. There may be better ways but I can at least, add and remove individual products as well as retrieve individual values. Unfortunately, I cant figure out how to access each of these from a different class. The most frequent error is being unable to access a non shared event or class. But when I share it, the error is, cant create list commands in a shared event. Is the code basically wrong, or am I trying to access the class incorrectly. Many thanks for your help.
OriginalGriff 31-Aug-23 9:28am    
At the moment, your list of products is an instance member of the Product class - which means that each Product has it's own, separate list. You don't want that, unless that list is meant to contain the sub-assemblies that make up a Product (like a mains lead, a manual, packing peanuts, and a box for example).

If New_Products is meant to contain a number of Products that aren't related to each other then either that needs to be created outside the class, or it needs to be Shared and the class needs to add and remove items from it automatically as they are created and destroyed. (The second option is the advanced version and adds a fair degree of complexity to the whole system - as a beginner, I'd suggest that the Product class shouldn't know about the collection at all so put it in your form class or similar.)
Member 15070471 31-Aug-23 10:50am    
Adding the entire list to the form class solved the problems. Many thanks for your help.
OriginalGriff 31-Aug-23 11:04am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900