Solved

generate dictionary at runtime from result set

Posted on 2011-09-20
3
250 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
ID: 36576077
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
ID: 36576483
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
ID: 36578312
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

20 Experts available now in Live!

Get 1:1 Help Now