Reputation: 1817
I just wonder if it's possible to change the key of a hashtable? And if so, how do i do it?
Here is the keys of my hashtable which i got by doing this:
var param = selectedGroup.Parameters.Keys;
so what i want to do is basically just change the key.
selectedGroup.Parameters.Keys[i].Value = newKey;
how do i do that?
I guess i have to move all the values beloning to that key somehow.
i tried to make a new hashtable like this
var parameters = new Tacton.Configurator.ObjectModel.SequencedHashtable<Tacton.Configurator.ObjectModel.Parameter>();
for (int i = 1; i < selectedGroup.Parameters.Count + 1; i++)
{
var para = result.Xml.SelectNodes("/session/view/parameters/param[" + i + "]/field")[0].InnerText + result.Xml.SelectNodes("/session/view/parameters/param[" + i + "]/desc-frags/frag[2]/@name")[0].Value;
var param = selectedGroup.Parameters[i];
parameters.Add(para, param);
}
but then i got the following error:
Unable to cast object of type 'Named`1[Tacton.Configurator.ObjectModel.Parameter,Tacton.Configurator.ObjectModel.Parameter]' to type 'Tacton.Configurator.ObjectModel.Parameter'.
i basically want the same hashtable as selectedGroup.Parameters but i want to change the keys from handsender_part_qty_multifieldITM_HANDSENDER_N002592U00U01 to handsender_part_qty_multifieldITM_HANDSENDER_N002592-00-01
this is what selectedGroup.Parameters[i] looks like:
Upvotes: 0
Views: 1956
Reputation: 33506
In most versions of "hashtables", there is no way of changing the key of an entry. This is because the key's hash value is very important. Changing key almost always changes the hash value and the entries would have to be moved around identically as if you removed old entry and re-added new entry with new key.
This is why, usually, there is no way of 'editing' the key. Instead, typical hashtables force you to expicitely remove and then add the entry back. Look at the classical .Net's Dictionary for an example of that.
Now, as for your piece of code, this is a pure guesswork. I couldn't find anything related to Tacton/SequencedHashtable on the internet, so I couldn't review its API, so from my point of view, there may be furry dragons inside and yourhash.Add(foo)
may format your harddrive just for fun.
However, basing on your requirement:
i basically want the same hashtable as selectedGroup.Parameters but i want to change the keys from handsender_part_qty_multifieldITM_HANDSENDER_N002592U00U01 to handsender_part_qty_multifieldITM_HANDSENDER_N002592-00-01
and on the "general idea of a hashtable" and on the assumption that this hashtable has some reasonable API that is designed so a typical .Net developer wouldn't bite his hands off using it, please try some typical things:
var parameters = new SequencedHashtable<Parameter>();
foreach(var par in selectedGroup.Parameters)
{
var newname = par.Name.Replace("U", "-");
// try changing the parameter's name:
par.Name = newname;
parameters.Add(par);
}
or
...
// try adding the parameter under new name:
par.Name = newname;
parameters.Add(newname, par);
...
or
...
// try cloning the parameter with new name:
var newpar = new Parameter(newname, par.config1, par.option2, par.foobar3, ...)
parameters.Add(newpar);
...
This is just a sketch. You may need to find the correct overload of 'Add' method. Maybe you will need to use 'Insert' instead. It may turn out that the hashtable doesn't support adding - then check its constructors. Maybe there's some constructor that takes a List of Parameters?
var parameters = new List<Parameter>(); <---- INTO a temporary list
foreach(var par in selectedGroup.Parameters)
{
var newpar = ..par or clone of par..
....edits, fixes to the newpar
parameters.Add(newpar);
}
var myNewParams = new SequencedHashtable<Parameter>( parameters );
etc. Unfortunatelly, most of the job is on your side - you have to review hte hashtable's API, you have to check if/how to edit the param's name, and/or how to clone the parameter so that all bits are preserved.
...or if you could point out where I/We can read the docs, then maybe someone will look at them. I have not found them, sorry.
Upvotes: 1