Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

generate dictionary at runtime from result set

Posted on 2011-09-20
3
254 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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