How to convert NameValueCollection to Hashtable - c#

I have a NameValueCollection object and I need to convert it to a Hashtable object, preferrably in one line of code. How can I achieve this?

You should consider using a generic Dictionary instead since it's strongly-typed, whereas a Hashtable isn't. Try this:
NameValueCollection col = new NameValueCollection();
col.Add("red", "rouge");
col.Add("green", "verde");
col.Add("blue", "azul");
var dict = col.AllKeys
.ToDictionary(k => k, k => col[k]);
EDIT: Based on your comment, to get a HashTable you could still use the above approach and add one more line. You could always make this work in one line but 2 lines are more readable.
Hashtable hashTable = new Hashtable(dict);
Alternately, the pre-.NET 3.5 approach using a loop would be:
Hashtable hashTable = new Hashtable();
foreach (string key in col)
{
hashTable.Add(key, col[key]);
}

It takes more than one line but it's decently simple
NameValueCollection nv = new NameValueCollection();
Hashtable hashTable = new Hashtable();
nv.Add("test", "test");
foreach (string key in nv.Keys)
{
hashTable.Add(key, nv[key]);
}
It compiles and executes as expected.

Related

Beginner python, about keys and array [duplicate]

I only want the Keys and not the Values of a Dictionary.
I haven't been able to get any code to do this yet. Using another array proved to be too much work as I use remove also.
How do I get a List of the Keys in a Dictionary?
List<string> keyList = new List<string>(this.yourDictionary.Keys);
You should be able to just look at .Keys:
Dictionary<string, int> data = new Dictionary<string, int>();
data.Add("abc", 123);
data.Add("def", 456);
foreach (string key in data.Keys)
{
Console.WriteLine(key);
}
Update for .NET 3.5+
To get list of all keys:
using System.Linq;
List<String> myKeys = myDict.Keys.ToList();
If you face any issues using System.Linq, see the following:
Visual Studio Does not recognize System.Linq
System.Linq Namespace
Marc Gravell's answer should work for you. myDictionary.Keys returns an object that implements ICollection<TKey>, IEnumerable<TKey> and their non-generic counterparts.
I just wanted to add that if you plan on accessing the value as well, you could loop through the dictionary like this (modified example):
Dictionary<string, int> data = new Dictionary<string, int>();
data.Add("abc", 123);
data.Add("def", 456);
foreach (KeyValuePair<string, int> item in data)
{
Console.WriteLine(item.Key + ": " + item.Value);
}
I can't believe all these convoluted answers. Assuming the key is of type: string (or use 'var' if you're a lazy developer): -
List<string> listOfKeys = theCollection.Keys.ToList();
The question is a little tricky to understand but I'm guessing that the problem is that you're trying to remove elements from the Dictionary while you iterate over the keys. I think in that case you have no choice but to use a second array.
ArrayList lList = new ArrayList(lDict.Keys);
foreach (object lKey in lList)
{
if (<your condition here>)
{
lDict.Remove(lKey);
}
}
If you can use generic lists and dictionaries instead of an ArrayList then I would, however the above should just work.
Or like this:
List< KeyValuePair< string, int > > theList =
new List< KeyValuePair< string,int > >(this.yourDictionary);
for ( int i = 0; i < theList.Count; i++)
{
// the key
Console.WriteLine(theList[i].Key);
}
For a hybrid dictionary, I use this:
List<string> keys = new List<string>(dictionary.Count);
keys.AddRange(dictionary.Keys.Cast<string>());
I often used this to get the key and value inside a dictionary: (VB.Net)
For Each kv As KeyValuePair(Of String, Integer) In layerList
Next
(layerList is of type Dictionary(Of String, Integer))

Get a vaule by key from nested hashtable

I have a nested Hashtable that looks like this.
Hashtable table = new Hashtable();
Hashtable subtable = new Hashtable();
Hashtable options = new Hashtable();
options.Add("file","foo");
subtable.Add("post_option",options);
table.Add(0,subtable);
//foreach here
This is what I have to work with, and I can't get any father up the chain to change what it is. So what I need to be able to do is get "foo" by calling for the key "file" starting from the "table" hashtable. I have tried every combo of foreach and .Keys and .Values. I just can't seem to get it lol. Thank you
Enumerator of Hashtable works similar to enumerator of IDictionary<TKey,TValue>, but it is not generic as it is ancient API came from .NET 1 where generics do not exist. So if you want to iterate over Hashtable with foreach you need to specify the type of item. In case of Hashtable it is DictionaryEntry.
foreach(DictionaryEntry tableEntry in table)
{
// your logic
}
If you do not know the keys of the first two tables, then you need to do something like this.
foreach(DictionaryEntry tableEntry in table)
{
Hashtable subtable = tableEntry.Value as Hashtable;
if (subtable == null)
continue;
foreach(DictionaryEntry subtableEntry in subtable)
{
Hashtable options = subtableEntry.Value as Hashtable;
if (options == null)
continue;
object file = options["file"];
}
}
You just need to do:
object foo = table[0]["post_option"]["file"];

How to convert Dictionary<> to Hashtable in C#?

I see many question/answers about how to convert a Hashtable to a Dictionary, but how can I convert a Dictionary to a Hashtable?
The easiest way is using constructor of Hashtable:
var dictionary = new Dictionary<object, object>();
//... fill the dictionary
var hashtable = new Hashtable(dictionary);
Dictionary<int, string> dictionary = new Dictionary<int, string>
{
{1,"One"},
{2,"Two"}
};
Hashtable hashtable = new Hashtable(dictionary);
Try this
Seems pretty rare to want to do, but at the simplest:
var hash = new Hashtable();
foreach(var pair in dictionary) {
hash.Add(pair.Key,pair.Value);
}
(assuming no unusual "implements typed equality check but not untyped equality check" etc)
You might want to consider using the Hashtable constructor overload that takes an IEqualityComparer parameter:
var hashtable = new Hashtable(dictionary, (IEqualityComparer) dictionary.Comparer);
In this way, your Hashtable uses the same Comparer as the dictionary. For example, if your dictionary used a case-insensitive string key, you might want your Hashtable to be case-insensitive too. E.g.:
var d = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
d.Add("a", "a");
d.Add("b", "b");
bool found;
found = d.ContainsKey("A"); // true
var hashtable1 = new Hashtable(d);
var hashtable2 = new Hashtable(d, (IEqualityComparer) d.Comparer);
found = hashtable1["A"] != null; // false - by default it's case-sensitive
found = hashtable2["A"] != null; // true - uses same comparer as the original dictionary

Can't get the array inside hashtable obj

I have an array of type Guid inside an HashTable
I get the values like the following but I can't get the array inside
IDictionaryEnumerator enumObj = moreTable.GetEnumerator();
while (enumObj.MoveNext())
{
foreach (var obj in enumObj.Value)
{
_guidList.Add(new Guid(obj.ToString()));
}
}
but this doesn't working with me any one know how to extract an array stored inside Hash table
You should use a type safe Dictionary and a generic List of Guid's instead:
Dictionary<Int32, Guid> guids = new Dictionary<Int32, Guid>();
guids.Add(1, new Guid("{25892e17-80f6-415f-9c65-7395632f0223}"));
guids.Add(2, new Guid("{e33898de-6302-4756-8f0c-5f6c5218e02e}"));
guids.Add(3, new Guid("{3a768eea-cbda-4926-a82d-831cb89092aa}"));
guids.Add(4, new Guid("{cd171f7c-560d-4a62-8d65-16b87419a58c}"));
guids.Add(5, new Guid("{17084b40-08f5-4bcd-a739-c0d08c176bad}"));
List<Guid> allGuids = new List<Guid>(guids.Values);
Assuming that your key is an integer, but that doesn't matter for the answer.
If you insist upon using a HashTable instead:
Hashtable guids = new Hashtable();
//fill Hashtable like above
ArrayList allGuids = new ArrayList(guids.Values);
foreach (Guid guid in allGuids) {
//do something with the GUID...'
}
[ all converted from VB.Net ]
I guess you should cast enumObj.Value to array, that should allow you to use it.

How to update C# hashtable in a loop?

I'm trying to update a hashtable in a loop but getting an error: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
private Hashtable htSettings_m = new Hashtable();
htSettings_m.Add("SizeWidth", "728");
htSettings_m.Add("SizeHeight", "450");
string sKey = "";
string sValue = "";
foreach (DictionaryEntry deEntry in htSettings_m)
{
// Get value from Registry and assign to sValue.
// ...
// Change value in hashtable.
sKey = deEntry.Key.ToString();
htSettings_m[sKey] = sValue;
}
Is there way around it or maybe there is a better data structure for such purpose?
you could read the collection of keys into another IEnumerable instance first, then foreach over that list
System.Collections.Hashtable ht = new System.Collections.Hashtable();
ht.Add("test1", "test2");
ht.Add("test3", "test4");
List<string> keys = new List<string>();
foreach (System.Collections.DictionaryEntry de in ht)
keys.Add(de.Key.ToString());
foreach(string key in keys)
{
ht[key] = DateTime.Now;
Console.WriteLine(ht[key]);
}
In concept I would do:
Hashtable table = new Hashtable(); // ps, I would prefer the generic dictionary..
Hashtable updates = new Hashtable();
foreach (DictionaryEntry entry in table)
{
// logic if something needs to change or nog
if (needsUpdate)
{
updates.Add(key, newValue);
}
}
// now do the actual update
foreach (DictionaryEntry upd in updates)
{
table[upd.Key] = upd.Value;
}
If you're using a Dictionary instead of a Hashtable, so that the type of the keys is known, the easiest way to make a copy of the Keys collection to avoid this exception is:
foreach (string key in new List<string>(dictionary.Keys))
Why are you getting an exception telling you that you've modified the collection you're iterating over, when in fact you haven't?
Internally, the Hashtable class has a version field. The Add, Insert, and Remove methods increment this version. When you create an enumerator on any of the collections that the Hashtable exposes, the enumerator object includes the current version of the Hashtable. The enumerator's MoveNext method checks the enumerator's version against the Hashtable's, and if they're not equal, it throws the InvalidOperationException you're seeing.
This is a very simple mechanism for determining whether or not the Hashtable has been modified. In fact it's a little too simple. The Keys collection really ought to maintain its own version, and its GetEnumerator method ought to save the collection's version in the enumerator, not the Hashtable's version.
There's another, subtler design defect in this approach. The version is an Int32. The UpdateVersion method does no bounds checking. It's therefore possible, if you make exactly the right number of modifications to the Hashtable (2 times Int32.MaxValue, give or take), for the version on the Hashtable and the enumerator to be the same even though you've radically changed the Hashtable since creating the enumerator. So the MoveNext method won't throw the exception even though it should, and you'll get unexpected results.
The simplest way is to copy the keys into a separate collection, then iterate through that instead.
Are you using .NET 3.5? If so, LINQ makes things a little bit easier.
The key part is the ToArray() method
var dictionary = new Dictionary<string, string>();
foreach(var key in dictionary.Keys.ToArray())
{
dictionary[key] = "new value";
}
You cannot change the set of items stored in a collection while you are enumerating over it, since that makes life very difficult for the iterator in most cases. Consider the case where the collection represents a balanced tree, and may well undergo rotations after an insert. The enumerate would have no plausible way of keeping track of what it has seen.
However, if you are just trying to update the value then you can write:
deEntry.Value = sValue
Updating the value here has no impact on the enumerator.
This is how I did it within a dictionary; resets every value in dict to false:
Dictionary<string,bool> dict = new Dictionary<string,bool>();
for (int i = 0; i < dict.Count; i++)
{
string key = dict.ElementAt(i).Key;
dict[key] = false;
}
It depends on why you are looping through the items in the hashtable. But you would probably be able to iterate throught the keys instead. So
foreach (String sKey in htSettings_m.Keys)
{ // Get value from Registry and assign to sValue.
// ...
// Change value in hashtable.
htSettings_m[sKey] = sValue;
}
The other option is to create a new HashTable. Iterate through the first while adding items to the second then replace the original with the new one.
Looping through the keys requires less object allocations though.
List<string> keyList = htSettings_m.Keys.Cast<string>().ToList();
foreach (string key in keyList) {
It is the same as the other answers, but I like the one line to get the keys.
Convert it to an array:
private Hashtable htSettings_m = new Hashtable();
htSettings_m.Add("SizeWidth", "728");
htSettings_m.Add("SizeHeight", "450");
string sKey = "";
string sValue = "";
ArrayList htSettings_ary = new ArrayList(htSettings_m.Keys)
foreach (DictionaryEntry deEntry in htSettings_ary)
{
// Get value from Registry and assign to sValue.
// ...
// Change value in hashtable.
sKey = deEntry.Key.ToString();
htSettings_m[sKey] = sValue;
}
private Hashtable htSettings_m = new Hashtable();
htSettings_m.Add("SizeWidth", "728");
htSettings_m.Add("SizeHeight", "450");
string sValue = "";
foreach (string sKey in htSettings_m.Keys)
{
// Get value from Registry and assign to sValue
// ...
// Change value in hashtable.
htSettings_m[sKey] = sValue;
}
Maybe you can use Hashtable.Keys collection? Enumerating through that might be possible while changing the Hashtable. But it's only a guess...

Categories