hashtable的一个使用

在开发食神软件,前台管理的点菜界面中,需要用到for循环动态生成各种菜的按钮和各种口味的按钮,而动态生成的按钮只有四个属性,而每一种菜和口味还有很多字段是下面还需要用到的!这时一可以给按钮加多需要的属性,二是建立hashtable表,把需要的属性组织封装到表中,并把表付给按钮的tag属性,代码如下:

                        Hashtable ht = new Hashtable();
                        ht.Add("KindNo", dsfood.Tables[0].Rows[j]["KindNo"].ToString());
                        ht.Add("CanFree", bool.Parse(dsfood.Tables[0].Rows[j]["CanFree"].ToString()));
                        ht.Add("CanDisc", bool.Parse(dsfood.Tables[0].Rows[j]["CanDisc"].ToString()));
                        ht.Add("IsService", bool.Parse(dsfood.Tables[0].Rows[j]["IsService"].ToString()));
                        ht.Add("IsSumPrnt", bool.Parse(dsfood.Tables[0].Rows[j]["IsSumPrnt"].ToString()));
                        ht.Add("IsPrint", bool.Parse(dsfood.Tables[0].Rows[j]["IsPrint"].ToString()));
                        ht.Add("IsLocal", bool.Parse(dsfood.Tables[0].Rows[j]["IsLocal"].ToString()));
                        ht.Add("IsCurrPrice", bool.Parse(dsfood.Tables[0].Rows[j]["IsCurrPrice"].ToString()));
                        ht.Add("Limited", int.Parse(dsfood.Tables[0].Rows[j]["Limited"].ToString()));

                        b[j].Tag = ht;//Tag属性选中的表

在下面需要用到的菜的属性时:

  Hashtable ht2 = b.Tag as Hashtable;  //解压表
                    limited = (int)ht2["Limited"]; //获得总的扣炖量  

                    model.KindNo = (string)ht2["KindNo"];
                    model.OrderEmpID = 149;
                    model.amt = model.Prise * model.qty;
                    model.CancelQty = 0;
                    model.IsFree = (bool)ht2["CanFree"];
                    model.FreeQty = 0;
                    model.IsDisc = (bool)ht2["CanDisc"];
                    model.IsServ = (bool)ht2["IsService"];
                    model.TotalAmt = model.Prise * model.qty;
                    model.IsSumPrnt = (bool)ht2["IsSumPrnt"];
                    model.IsPrint = (bool)ht2["IsPrint"];
                    model.IsLocal = (bool)ht2["IsLocal"];

哈希表是一个元素<j键(key),值(value)对的集合>每个元素都是存储在Dictionnary对象中的键/值对,键不能为空引用null,但是值可以为null

在需要存储具有键/值对属性的数据时,例如<学好,学生对象>等具有一一对应关系的数据时,常常需要使用哈希表

原文地址:https://www.cnblogs.com/zhang123/p/2809413.html