0

我想将哈希表分配给数组结构请帮助下面是代码

    public void SendCmdWithData(int IntCode, Hashtable Htbl) 
    {
        DatawithCommandCode[] arrData = new DatawithCommandCode[Htbl.Count];

        try {
            for (int intVal = 0; intVal < Htbl.Count - 1; intVal++) {
            }
        }
        catch (Exception) {}
    }

我的结构

    public struct DatawithCommandCode
    {
        public int CmdCode;
        public int Value;
    }
4

1 回答 1

0

你可以像下面这样实现它:

 public static void SendCmdWithData(int IntCode, Hashtable Htbl)
        {
            DatawithCommandCode[] arrData = new DatawithCommandCode[Htbl.Count];
            try
            {
                int i = 0;
                foreach (System.Collections.DictionaryEntry element in Htbl)
                {
                    DatawithCommandCode tempData = new DatawithCommandCode();
                    tempData.CmdCode = Convert.ToInt32(element.Key);
                    tempData.Value = Convert.ToInt32(element.Value);
                    arrData[i++] = tempData;
                }
            }
            catch (Exception) { }
        }

但我希望你使用 .Net framework 2.0 或更高版本,然后最好使用Dictionary

于 2012-09-26T09:35:24.053 回答