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.
Related
I have a variable like below:
var dict = new Dictionary<string, HashSet<string>>();
I want to make sure the values in the HashSet are unique using StringComparer.Ordinal. What would be the syntax for that? Thanks.
Everytime you initialize a new key, the value should be passed an explicit parameter like this:
dict["some key"] = new HashSet<string>(StringComparer.Ordinal);
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"];
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
Hey all is there a collection type like arrayList which i can add an object to using an ID?
effectively as the title of my post sugests a Direct object collection. so for example:
DirectCollection.addAt(23, someobject);
and
DirectCollection.getAt(23);
etc etc
i know arraylist is usable in that case but i have to generate the initial entry with a null reference object and if if the object has an ID like 23 i have to generate 22 other entries just to add it which is clearly impractical.
basically using the object position value as a unique ID.
Any thoughts?
Many thanks.
You could use Dictionary<int, YourType>
Like this:
var p = new Dictionary<int, YourType>();
p.Add(23, your_object);
YourType object_you_just_added = p[23];
Use a dictionary.
Dictionary<int, YourType>
It allows you to add/get/remove items with a given key and non continuous ranges.
You could use a Dictionary
The code for your example would be very simple:
Dictionary<int, AType> directCollection = new Dictionary<int, AType>();
directCollection.Add(23, someObjectOfAType);
AType anObject = directCollection[23];
I think KeyedCollection or Dictionary is what you need.
Use System.Collections.Hashtable. It allow to store the heterogeneous type of object (a Hashtable can hold the multiple type of object).
Example:
System.Collections.Hashtable keyObjectMap = new System.Collections.Hashtable();
//Add into Hashtable
keyObjectMap["Key_1"] = "First String";
keyObjectMap["Key_2"] = "Second String";
//Add the value type
keyObjectMap["Key_3"] = 1;
keyObjectMap["Key_4"] = new object();
//Get value/object from Hashtable
string value = (string)keyObjectMap["Key_2"];
int intValue = (int)keyObjectMap["Key_3"];
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.