Solved

generate dictionary at runtime from result set

Posted on 2011-09-20
3
247 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:crafuse
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
Comment Utility
Try this

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

Cards = result.ToDictionary(Function(k) k.key, Function(v) v.val)
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
Comment Utility
First of all, you declaration should be :

Dim cards As New Dictionary(Of Integer, String)

The way you are declaring it, it is not a Dictionary object.  it is an Object variable to which you have assigned a Dictionary. Working with Object variables is not only awkward, but also slows down the performance a lot.
-----

Declare cards as public in the declaration of a module. It will be available in all the application.

Module Module1

Public cards as New Dictionary(Of Integer, String)

' Module code

End Module

You can initialize it at the declaraion point, as you did for the Dim, or initialize anywhere else in your code.

cards.Add(0, "No Card name")
cards.Add(1, "Boris Skullcrusher")

or if you read the data from somewhere else:

cards.Add(tbl_cards.rows(0).card_id,tbl_cards.rows(0).cardname)
cards.Add(tbl_cards.rows(1).card_id,tbl_cards.rows(1).cardname)

or whathever syntax you need to access your data.

Be aware of a big potential problem with a Public variable though. If you have a bug related to it, you have to look everywhere in the application. That is why you might have understood that you needed a class, and it would be a better solution for a professional application. But a public variable is the simplest way.
0
 

Author Closing Comment

by:crafuse
Comment Utility
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now