Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

working with KeyCollection

Posted on 2013-11-13
10
Medium Priority
?
803 Views
Last Modified: 2013-11-21
Hi,

I'm trying to figure out this dictionary keyCollection stuff.  All I want is to get a list of all the keys of a dictionary, which is defined as:

private baseDictionary As Dictionary(Of String, Dictionary(Of String, BasicContainer))

where BasicContainer is a small class.  What I don't understand is why baseDictionary.Keys() returns something called a KeyCollection instead of a simple list of string?  What's the purpose of that?  If I have Dictionary(Of String, Tvalue), then does it really matter what Tvalue is?  It could be a million different things, but the keys would all still be string. What is the point of this KeyCollection?

This definition of a strongly type KeyCollection makes no sense to me.  How can I get a simple List(OF String) or ArrayList or some basic, sensible collection of strings that represent the dictionary keys?

Thanks!
0
Comment
Question by:ugeb
[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
  • 5
  • 5
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39648143
Try using the ToList extension method:

Dim list As New Dictionary(Of String, Integer)() From { 
	{"John", 1}, 
	{"Dave", 2}, 
	{"Greg", 3} 
}

Dim keys = list.Keys.ToList()

Dim text = String.Join(",", keys.ToArray())

Console.WriteLine(text)

Open in new window

0
 
LVL 11

Author Comment

by:ugeb
ID: 39648928
Maybe it's the version of vb.net, but I don't have a ToList method in my dictionaries.  I'm bound to vb.net 3.0.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39649126
I overlooked a little of the magic code that made that work:

using System.Linq;

Without that imports at the top of the code module, ToList will not work, since it is provided by that namespace.  I believe that 3.0 has the LINQ extensions.  You will need to check that, and let me know if I am correct.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Author Comment

by:ugeb
ID: 39649619
It looks like 3.0 doesn't have linq as I get an error on the using statement. There's a ToArray method available, but in any case it looks like I may just have to continue brute force conversion.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39649918
That method was introduced in 3.5.  It might be better to use 3.5, rather than 3.0, but that might introduce some breaking changes.
0
 
LVL 11

Author Comment

by:ugeb
ID: 39650313
Unfortunately I'm using a critical library (NDde) that was written for .net 2.0.  I've managed to get .net 3.0, but no further, so 3.5 won't work.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39651052
Then, I believe that you will need to use a for loop:

For Each key As String In baseContainer.Keys
0
 
LVL 11

Author Comment

by:ugeb
ID: 39654808
Yes, that was the obvious way and it's what I'm currently doing, but it feels like a big hack and it's slower, uglier, less elegant and less efficient and than a native procedure. That's why I wanted to change.  I was using this method before I even asked the question, but I really wanted to know if there was a better way.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 39654832
Hmmm...another possibility, that I haven't thought about in a long time:

Dictionary.KeyCollection.CopyTo Method
http://msdn.microsoft.com/en-us/library/91e2z0hz(v=vs.85).aspx

string[] keys = new string[dictionary.Keys.Count];
dictionary.Keys.CopyTo(keys, 0);

Open in new window

0
 
LVL 11

Author Closing Comment

by:ugeb
ID: 39666873
I didn't even see that you had posted this response until now.  I don't have time to check it out now, but I do see it works in .net 3, so it looks hopeful.  Thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

661 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