Solved

generate dictionary at runtime from result set

Posted on 2011-09-20
3
256 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
[X]
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
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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