Donnie Archer
Donnie Archer

Reputation: 1

Multidimensional Arrays using Dictionaries

I am trying to make a multidimensional associative array. I want it so I can have something like:

someVar(date)(hour)(category) = mssql query

I am using the following to try and prepare but am having trouble adding data to the array.

Dim test As New Dictionary(Of Integer, Dictionary(Of String, String))
Dim test2 As New Dictionary(Of String, String)

Any help is greatly appreciated.

-----EDIT: Here is what I am using, it works as desired. Doe ayone see why this would be a bad way to do it?

Dim test As New Dictionary(Of Integer, Dictionary(Of String, String))
Dim SomeNum As Integer = 0
Dim someStr As String = "This is a string: "


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    While SomeNum < 100
        Dim someNum2 As Integer = 0
        Dim test2 As New Dictionary(Of String, String)

        While someNum2 < 100
            test2.Add(CType(someNum2, String), someStr & CType(someNum2, String))

            someNum2 += 1
        End While
        test.Add(SomeNum, test2)
        SomeNum += 1


    End While

    For Each kvp As KeyValuePair(Of Integer, Dictionary(Of String, String)) In test
        Dim ccc As String = ""
        Dim ddd As String = ""
        Dim v1 As String = CType(kvp.Key, String)
        Dim v2 As Dictionary(Of String, String) = kvp.Value

        lblOne.Items.Add("Key: " & v1)

        For Each kvp2 As KeyValuePair(Of String, String) In v2

            Dim v3 As String = kvp2.Key
            Dim v4 As String = kvp2.Value

            lblTwo.Items.Add("SubKey: " & v3 & " Value: " & v4)

            lblOne.Items.Add("")

        Next

        lblOne.Items.Add(v1 & " End--------------")
        lblTwo.Items.Add(v1 & " End--------------")
    Next

End Sub

Upvotes: 0

Views: 2530

Answers (3)

Victor Zakharov
Victor Zakharov

Reputation: 26454

A dictionary of dictionary of dictionaries is a maintenance nightmare, to query and debug.

I suggest to have a composite key instead, so instead of doing 3 lookups you would just do one for a string of date+hour+category. For example, date=Monday, hour=9PM, category=Apples, your key is Monday:9PM:Apples (I picked colon as parts separator, but you can choose a different character).

Upvotes: 0

Nikita Silverstruk
Nikita Silverstruk

Reputation: 1117

Create a class with properties "Date", "HourlySales", "Category".

Public Class Sales
    Public Property SalesDate() As Date
    Public Property HourlySales() As Decimal
    Public Property Category() As String

    Public Sub New()
    End Sub

    Public Sub New(vSalesDate As Date, vHourlySales As Decimal, vCategory As String)
        SalesDate = vSalesDate
        HourlySales = vHourlySales
        Category = vCategory
    End Sub
End Class

Create a list of objects of type Sales

Shared Function GetSales() As List(Of Sales)
        Dim SalesList As New List(Of Sales)

        Using connection As New SqlConnection(YourConnectionString)
            Dim cmd As SqlCommand = New SqlCommand("SelectSalesList", connection)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection.Open()

            Dim reader As SqlDataReader = cmd.ExecuteReader()

            While reader.Read
                SalesList.Add(New Sales(reader("SalesDate"), reader("HourlySales"), reader("Category")))
            End While
        End Using

        Return SalesList
    End Function

You can call the GetSales() function to return a list of Sales.

Upvotes: 1

OneFineDay
OneFineDay

Reputation: 9024

Look into Entity Framework it makes objects out of your database.

MSDN EF

For a custom array, you may find a Tuple useful.

Upvotes: 0

Related Questions