根据值匹配返回字典键

本文关键字:返回 字典 | 更新日期: 2023-09-27 18:08:03

我有这个巨大的条目表/字典。

我想根据匹配的字符串值返回Int16键值。我正在尝试根据组合框中选择的字符串自动建议Int16值。

我的READ_TABLE函数没有任何问题,这是retrieve_ival函数给我的问题。

我试图根据我从组合框中选择的字符串设置数字updown的值,但我想从字典中获取该值,而不是再次存储我已经存在的值。我想错了吗?

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?
    }
}

根据值匹配返回字典键

如果您交换键和值,即使用Dictionary<string, Int16>,对您的程序是否可行?

如果不是,请记住Dictionary<TKey,>也是键值对KeyValuePair<TKey, TValue>的集合。因此,您可以使用Linq查找具有匹配字符串值的键值对,从而访问其键:

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;

注意KeyValuePair<Int16,>是一个结构体。因此,当FirstOrDefault(…)未能找到匹配的键值对时,它将返回一个KeyValuePair<int,>结构体,其字段设置为各自的默认值。该结构体中的整型键为0,字符串值为null。因此,if语句检查键值对的字符串值是否为null,以检测"未找到匹配的键值对"的条件。

当然,只有当您不将null值存储在字典中时,这才会可靠地工作。如果您的字典允许使用null值,则不要使用FirstOrDefault(…),而是使用foreach循环遍历键值对以查找值与给定字符串匹配的值。