Link to home
Start Free TrialLog in
Avatar of crafuse
crafuseFlag for Canada

asked on

generate dictionary at runtime from result set

Experts -

I'm using this dictionary in the following way to populate a listbox:

Dim cards = New Dictionary(Of Integer, String) From
       {
       {0, "No Card name"},
       {1, "Boris Skullcrusher"},
       {2, "Amber Rain"},
       {3, "Victor Heartstriker"},
       {4, "Gwenneth Truesight"},
       {5, "Nishaven"},
       {6, "Eladwen Frostmire"},
       {7, "Jericho Spellbane"},
       {8, "Zhanna Mist"},
       {9, "Lance Shadowstalker"},
       {10, "Serena Thoughtripper"},
       {11, "Ter Adun"},
       {12, "Logan Stonebreaker"},
       }

        If strLibraryList <> "" Then

            Dim idLibraryList = strLibraryList.Split(",".ToCharArray()).ToList().ConvertAll(Function(c) Convert.ToInt32(c)).ToList()

            Dim countList = New Dictionary(Of String, Integer)

            Dim result = (From id In idLibraryList
                          Where cards.Keys.Contains(id)
                          Group id By Key = id Into Group
                          Order By Key
                          Select String.Format("{0,-20}{1}{2}", cards(Key), vbTab & "    ", Group.Count())).ToArray()

            lstLibraryCards.Items.AddRange(result)

        End If

I'm using this dictionary in a few different places. I would like to be able to generate this dictionary at runtime from a result set and have it accessable throughout my app. I thought I might simply be able to generate the dictionary list with a formatted query, but that doesn't work. Any ideas?

From what I'm reading I guess I'd have to do this in a new class? I've never created my own class, so not sure where to start...

The values in the dictionary are card_id and card_name from a tbale called tbl_cards.

TIA
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crafuse

ASKER

i went this route:

Public Class MerchantCardsClass

    Public Shared cards As New Dictionary(Of Integer, String) From
          {
              }
End Class


and then on my logon form load event i grab the data i need and then build the dictionary values like this:

        Do While drReader.Read()
            MerchantCardsClass.cards.Add(CInt(drReader.Item("cardno").ToString), drReader.Item("name").ToString)
        Loop

I guess i didn't explain myself very well in the question. i simply needed a way to build the values dynamically, and then after posting the question this simple solution popped into my head.

;-)

thnx.