Queue.Contains

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
I know i can use .Contain to search a queue for a string. But how do you search a queue with a 2d array? I dont need to use a loop, do i?

VB.NET:
Dim a As New Queue(Of String)(10)
Dim b As New Queue(Of String())(10)
a.Enqueue("aa")
b.Enqueue({"aa","bb"})

if a.Contains("aa") then

end if
 
You can't just queue("test").contains("string")

Nowhere near a PC to check but think that should work for a 2 dimension array

Sent using XT910 with Android 4.0.4 and MIUI 2.8.10
 
"Value of type String cannot be converted to 1-dimensional array of String" is the error i get when i tried that.

VB.NET:
Dim b As New Queue(Of String())(10)
b.Enqueue({"aa","bb"})

if b.Contains("aa") then

end if

essentially this is what i am trying to do, but is there a built-in command to do it?

VB.NET:
    Private Function searchQueue(name As String) As Boolean
        For i As Integer = 0 To dlData.Count - 1
            If name = b.ElementAt(i).ElementAt(0) Then
                Return True
            End If
        Next
        Return False
    End Function
 
Last edited:
This is another slightly odd question in a series of slightly odd questions. A Queue with a 2D array? There's no 2D arrays here. I think that it might be time that you actually describe what you're doing rather than using all these contrived examples because I suspect that there may be a better way. What exactly does this data represent?
 
You have a queue of string arrays containing 10 string arrays of dynamic size. I am guessing that is not what you really want. If it is, and if you want to know if any of the 10 string arrays contains a single string, then you need to loop through each item of the queue and call the .Contains method on each string array, then OR the 10 resulting booleans.
 
What i have is a queue of size 10, contain 1d array of size 2. The Code below does search each array's first value for the string i want. I am just asking if i can use .Contain or other command to search a queue of array , like i did with a queue of string. Instead of doing a loop.

VB.NET:
Dim b As New Queue(Of String())(10)
b.Enqueue({"aa","bb"})
b.Enqueue({"cc","dd"})

If searchQueue("aa") then
end if

Private Function searchQueue(name As String) As Boolean
        For i As Integer = 0 To dlData.Count - 1
            If name = b.ElementAt(i).ElementAt(0) Then
                Return True
            End If
        Next
        Return False
End Function
 
If you're not prepared to provide the information I asked for then all I can say is that the answer is "no". The items in the Queue are String arrays so it's Contains method can only tell you whether it contains a specific String array. That's the same for any list. Because arrays are reference types, that would also have to be the same array object that you queued in the first place, not a different array containing the same elements.
 
The data represent a Name and a date. And i only want to retain 10 of them. So i use a queue of 10 to store each pair of data.
 
Right then. As I suspected, a String array is very bad way to store the data. You should be defining a type, i.e. a class or a structure, with two properties, one of type String and one of type Date, and then storing instances of that type in your collection. I wouldn't use a Queue either. Queues are designed specifically to be access in FIFO order. It sounds like you actually want random access. I would suggest just using a List. You can Add to the List and Remove the first item when it grows too large. You could even define your own custom class that inherits Collection(Of T) and adds the size limit to the class itself. You could then set a property to the maximum size and let the collection take care of the rest.

Having said all that, it just occurred to me how you could implement the sort of "contains" functionality you want, regardless of the type of items. You can use the Any extension method. Here's an example that determines whether the list contains a String array with a first element of a particular value:
VB.NET:
If myList.Any(Function(arr) arr(0) = someValue) Then
I would still recommend defining your own type though. That code will work in that situation too.
 
The no-brainer here is to use a DataTable, because that is exactly what you need anyways. Create a table, add two columns TheName and TheDate. Add your records to it and query through the .Select method. Done. Highly scalable, fast, portable, and easy to manage.
 
Here's the sort of thing I was talking about:
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim list As New SizeLimitedCollection(Of Item) With {.MaxCount = 10}

        list.Add(New Item With {.Name = "Name1", .Date = #1/1/2000#})
        list.Add(New Item With {.Name = "Name2", .Date = #1/2/2000#})
        list.Add(New Item With {.Name = "Name3", .Date = #1/3/2000#})
        list.Add(New Item With {.Name = "Name4", .Date = #1/4/2000#})
        list.Add(New Item With {.Name = "Name5", .Date = #1/5/2000#})
        list.Add(New Item With {.Name = "Name6", .Date = #1/6/2000#})
        list.Add(New Item With {.Name = "Name7", .Date = #1/7/2000#})
        list.Add(New Item With {.Name = "Name8", .Date = #1/8/2000#})
        list.Add(New Item With {.Name = "Name9", .Date = #1/9/2000#})

        DisplayCollectionInformation(9, list)

        list.Add(New Item With {.Name = "Name10", .Date = #1/10/2000#})

        DisplayCollectionInformation(10, list)

        list.Add(New Item With {.Name = "Name11", .Date = #1/11/2000#})

        DisplayCollectionInformation(11, list)
    End Sub

    Private Sub DisplayCollectionInformation(itemsAddedCount As Integer, collection As SizeLimitedCollection(Of Item))
        Dim info As New List(Of String)

        info.Add("Count: " & collection.Count)
        info.Add("First Item Name: " & collection.First().Name)
        info.Add("First Item Date: " & collection.First().Date)
        info.Add("Last Item Name: " & collection.Last().Name)
        info.Add("Last Item Date: " & collection.Last().Date)

        MessageBox.Show(String.Join(Environment.NewLine, info), "Items Added: " & itemsAddedCount)
    End Sub

End Class


Public Class Item
    Property Name() As String
    Property [Date]() As Date
End Class

Public Class SizeLimitedCollection(Of T)
    Inherits ObjectModel.Collection(Of T)

    Property MaxCount() As Integer = Integer.MaxValue

    Protected Overrides Sub InsertItem(index As Integer, item As T)
        If Count = MaxCount Then
            RemoveAt(0)

            If index > 0 Then
                index -= 1
            End If
        End If

        MyBase.InsertItem(index, item)
    End Sub

End Class
 
The no-brainer here is to use a DataTable, because that is exactly what you need anyways. Create a table, add two columns TheName and TheDate. Add your records to it and query through the .Select method. Done. Highly scalable, fast, portable, and easy to manage.

I would be reluctant to use a DataTable unless it was in conjunction with ADO.NET anyway but, with the advent of LINQ, it becomes even less necessary. Many applications actually define their own entity classes even they are using ADO.NET and they transfer the data out of the DataTables as early as possible and into them as late as possible. Not defining your own type in a situation where you don't need to use a DataTable is a bit of a hack really.
 
Can you explain your reluctance? I just love datatables for general purpose data storing like that, even unbound, because they require near-zero infrastructure, very little code, are as strongly typed as a custom type, are easy to expand (add a column...) and provide everything you need to do any basic querying, importing, or exporting. I fail to see what building a custom type buys me, if not more code, and less flexibility.

I can understand the use of a bloat like Entity Framework in situations where the data schema is complex and the amount of data is large and not changing a lot, from a management point of view, but 2 columns and 10 rows hardly justifies the amount of support code needed.
 
Last edited:
I just love datatables ... because they ... are as strongly typed as a custom type
Given that every field in a DataRow, accessed via its Item property, is an Object reference, that statement is untrue and that's probably the primary reason not to use a DataTable. The DataTable is very general-purpose because it enables ADO.NET to map any schema from any database. When you're working with specific data that you know the schema for at design time, the right thing to do is write code specific to that data. When a type to represent one record takes four lines to write, how much justification do you need?
 
Wouldn't this be a lot more complicated? It seems like a lot more coding to create my own type than using the existing type? The date as a string is fine since i don't need to search the date, just to display it. The name is the one i need to see if it exist. So i don't add the same name twice on different date. And i need to store the data into My.Settings afterward. I had to convert the Queue to a delimited string and store it to Settings.
Can the list type be store directly to My.Settings?
 
Last edited:
Back
Top