Reputation: 89
I have this huge item table / dictionary.
I want to return the Int16
key value based on the matched string value. I'm trying to auto suggest the Int16
value based on chosen string in combo box.
I'm not having any issues with my READ_TABLE
function, it's the retrieve_ival
function giving me problems.
I'm trying to have the numeric updown's value set based on the string I choose from the combo box, but I want to take that value from the dictionary opposed to storing the values again I already have present. Am I going all wrong about this?
public static Dictionary<Int16, string> m_ItemLookup = new Dictionary<Int16, string>
{
{ 00, "Handgun"},
{ 01, "Handgun SG"},
{ 02, "Handgun GL" },
{ 03, "Magnum Handgun"},
{ 04, "Magnum Revolver"},
{ 05, "Handgun HP"},
{ 06, "Model 600"},
{ 07, "Revolver"},
{ 08, "Burst Handgun"},
{ 10, "Submachine gun"},
{ 11, "Shotgun"},
{ 12, "Shotgun E"},
{ 13, "Special Hunting Gun"},
{ 14, "Assault Rifle"},
{ 17, "Pill Shooter(White)"},
{ 18, "Pill Shooter(Blue)"},
{ 19, "Mine Detector"},
{ 20, "Rocket Launcher"},
{ 23, "Grenade Launcher (Burst)"},
{ 24, "Grenade Launcher (Flame)"},
{ 25, "Grenade Launcher (Acid)"},
{ 26, "Grenade Launcher (??)"},
{ 27, "Grenade Launcher (BOW GAS)"},
{ 28, "Pesticide Spray"},
{ 29, "Flame Spray"},
{ 30, "Nail Gun"},
{ 31, "Ampoule Shooter"},
{ 100, "Survival Knife"},
{ 101, "Folding Knife"},
{ 102, "Butcher Knife"},
{ 103, "Iron Pipe"},
{ 104, "Curved Iron Pipe"},
{ 105, "Bent Iron Pipe"},
{ 106, "Long Pole"},
{ 107, "Square Timber"},
{ 108, "Stick"},
{ 109, "Axe"},
{ 110, "Scrub Brush"},
{ 111, "Wooden Pole"},
{ 112, "Stick"},
{ 113, "Spear"},
{ 114, "Molotov Cocktail"},
{ 115, "Hammer"},
{ 116, "Crutch"},
{ 117, "Stun Rod"},
{ 118, "Concrete Piece"},
{ 119, "Broken Crutch"},
{ 155, "Empty Bottle"},
{ 157, "Chemical Solvent (green)"},
{ 158, "Yellow Chemical Bottle"},
{ 159, "Gray Chemical Bottle"},
{ 160, "Gray Chemical Bottle"},
{ 200, "Handgun Magazine"},
{ 201, "Handgun SG Magazine"},
{ 202, "Handgun GL Magazine"},
{ 203, "Magnum Clip"},
{ 204, "Revolver Speed Loader"},
{ 205, "Submachine Gun Clip"},
{ 206, "Assault Rifle Clip"},
{ 250, "Handgun Rounds 9mm"},
{ 251, "Magnum HG Rounds"},
{ 252, "Magnum Revolver Rounds"},
{ 253, "Revolver Rounds"},
{ 254, "Shotgun Rounds"},
{ 255, "Rifle Rounds"},
{ 300, "Green Herb" },
{ 301, "Blue Herb" },
{ 302, "Red Herb" },
{ 303, "Green + Green Mixed Herb" },
{ 304, "Green + Green + Green Mixed Herb" },
{ 305, "Green + Red Mixed Herb" },
{ 306, "Green + Blue Mixed Herb" },
{ 307, "Green + Green + Blue Mixed Herb" },
{ 308, "Green + Red + Blue Mixed Herb" },
{ 309, "First Aid Spray" },
{ 310, "Recovery Medicine"},
{ 311, "Hemostat"},
{ 312, "Day Light" },
{ 313, "Blue + Red Mixed Herb"},
{ 314, "Antidote"},
{ 315, "Large Recovery Medicine"},
{ 316, "Anti Virus Pill"},
{ 317, "Large Anti Virus"},
{ 318, "Recovery Medicine Base"},
{ 450, "Lighter"},
{ 451, "Alcohol Bottle"},
{ 452, "Bottle + news Paper"},
{ 453, "Broken Handgun"},
{ 454, "Broken Shotgun"},
{ 455, "Battery"},
{ 456, "Broken Handgun SG"},
{ 12600, "Auxiliary Building Key" },
{ 12601, "Adminstrator's Office Key"},
{ 12602, "Rusty Key"},
{ 12603, "Syringe (empty)"},
{ 12604, "Syringe (Solvent)"},
{ 12605, "Pendant"},
{ 14000, "Bolt Cutters"},
{ 14001, "Elephant Key"},
{ 14002, "Alligator Key"},
{ 14003, "Lion key"},
{ 14004, "Office Key"},
{ 14005, "Mr Racoon Medal"},
{ 14006, "Lion Emblem (Red)"},
{ 14007, "Lion Emblem (Blue)"},
{ 14008, "Blank Tape"},
{ 14009, "Parade BGM Tape"},
{ 14010, "Empty entry"}
};
public void READ_TABLE(string fp, ListView LV, ComboBox CMB, bool f_type, RichTextBox Debug_Log)
{
LV.Items.Clear();
FileStream fs = new FileStream(fp, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
string[] item_names = new string[0];
int null_count = 0;
fs.Seek(0, SeekOrigin.Begin);
TBL_HEADER.tag = br.ReadInt32();
TBL_HEADER.Item_count = br.ReadInt32();
Debug_Log.AppendText("Defined Item Count: " + TBL_HEADER.Item_count.ToString());
if (f_type)
{
TBL_HEADER.Item_count *= 3;
}
Array.Resize(ref TBL_ITEM, TBL_HEADER.Item_count);
Array.Resize(ref item_names, TBL_HEADER.Item_count);
for (int i = 0; i < TBL_HEADER.Item_count; i++)
{
TBL_ITEM[i].Item_val = br.ReadInt16();
TBL_ITEM[i].Quantity = br.ReadByte();
TBL_ITEM[i].occurence = br.ReadByte();
LV.Items.Add(i.ToString());
LV.Items[i].SubItems.Add(TBL_ITEM[i].Item_val.ToString());
if (TBL_ITEM[i].Item_val == -1)
{
null_count +=1;
LV.Items[i].SubItems.Add("Null");
LV.Items[i].ForeColor = Color.DarkRed;
LV.Items[i].Font = new System.Drawing.Font("Lucida Console", 9);
}
if (m_ItemLookup.ContainsKey(TBL_ITEM[i].Item_val))
{ LV.Items[i].SubItems.Add(m_ItemLookup[TBL_ITEM[i].Item_val]);
LV.Items[i].SubItems.Add(TBL_ITEM[i].Quantity.ToString());
LV.Items[i].SubItems.Add(TBL_ITEM[i].occurence.ToString());
}
}
foreach (string item_name in m_ItemLookup.Values) // add dictionary values to combo box
{
CMB.Items.Add(item_name);
}
Debug_Log.AppendText("\n Total null items Read: " + null_count.ToString());
fs.Close();
br.Close();
}
public void retrieve_ival(ComboBox cmb, NumericUpDown Ival, NumericUpDown idx)
{
int set_key;
if (m_ItemLookup.ContainsValue(cmb.SelectedText))
{
Ival.Value = m_itemLookup; //how do i return key? of the matched value?
}
}
Upvotes: 2
Views: 1877
Reputation:
Would it be feasible for your program if you swap the keys and values, i.e., using a Dictionary<string, Int16>
?
If not, remember that a Dictionary<TKey, TValue> is also a collection of key-value pairs KeyValuePair<TKey, TValue>
. You could therefore use Linq to find a key-value pair with a matching string value and thus get access to its key:
KeyValuePair<Int16, string> kvp = m_ItemLookup.FirstOrDefault(x => x.Value == desiredStringValue);
if (kvp.Value == null)
{
... no matching key-value pair found
}
int set_key = kvp.Key;
Note that KeyValuePair<Int16, string> is a struct. Hence, when FirstOrDefault(...) fails to find a matching key-value pair, it will return a KeyValuePair<int, string> struct with its fields set to the respective default values. The integer key in this struct would be 0
, and the string value would be null
. Hence the if
statement which checks whether the string value of the key-value pair is null
to detect the 'no matching key-value pair found' condition.
This of course will only work reliably as long as you do not store null
values in the dictionary. If null
values would be permissible for your dictionary, then do not use FirstOrDefault(...), but instead use a foreach
loop to iterate over the key-value pairs to find one whose value matches the given string.
Upvotes: 3