积极答复者
hashtable 键值对问题

问题
-
实现一个购物按钮的操作.当我进入一个产品页面 有一个按钮添加购物车. 我用如下代码
int shopCarid = sid; //获取产品的id sid通过session传递过来的.
if (Session["ShopCar"] == null)
{
//如果用户没有分配购物车
ht = new Hashtable();//新生成一个
ht.Add(shopCarid, DropCountt.SelectedValue); //添加一个商品
Session.Add("ShopCar", ht); //分配给用户
}
else
{
ht = (Hashtable)Session["ShopCar"];//得到购物车的哈希表
if (ht.Contains(shopCarid)) //购物车中已有此商品,商品数量加1
{
int cout = Convert.ToInt32(ht[shopCarid].ToString());//得到该商品的数量
ht[shopCarid] = cout + Convert.ToInt32(DropCountt.SelectedValue);//商品数量加1
}
else
{
ht.Add(shopCarid, DropCountt.SelectedValue);//如果没有此商品,则新添加一个项
}
}
foreach (DictionaryEntry each in ht)
{
string str = "编号" + shopCarid + "商品有" + ht[shopCarid].ToString() + "个</br>";
Response.Write(str);
}
// Response.Redirect("basket.aspx");
}
我的问题是我才用hashtable 可以记录产品的编号.和我页面有个产品数量的选择.如果还有一个产品的尺码选择框.尺码分3种型号.我这个 hashtable 怎么记录我选择的型号.类似我选择的数量那样.切产品页面是更具类别来判断产品是否有尺寸这个下拉框的.包类就没有尺寸只有数量.而鞋要有尺寸和数量.我的ht.add(产品id,选择数量); 尺寸怎么记录啊??
不用羡慕----那,只是个传说!
答案
-
您好,两种思路供参考:
1、即使相同的产品名称,如果尺码不同则采用不同的产品ID,保证产品ID只描述一种特征的产品
2、将您认为关键的产品特性整合在一起设计成一个结构,用异或成员重写GetHashCode方法。将这个结构作为主键。- 已标记为答案 邹俊才Moderator 2009年8月26日 6:57
-
1 使用 Dictionary<TKey, TValue>, 定义一个类包含产品 Id, 数量,将这个类存入到 Value 中,(存到 Hashtable 的 Value 中也行,使用的时候再转换一次),不过建议 Dictionary 使用如下
public class Product { public Int32 ProductId { get; set; } public Int32 Amount { get; set; } public String Size { get; set; } public Product(Int32 fProductId, Int32 fAmount, String fSize) { this.ProductId = fProductId; this.Amount = fAmount; this.Size = fSize; } }
-- 使用Dictionary<String, Product> dic = new Dictionary<string, Product>(); dic.Add(1, new Product(1,1,"XL")); dic[1].Amount++;
2 使用 DataTable
知识改变命运,奋斗成就人生!- 已标记为答案 邹俊才Moderator 2009年8月26日 6:57
-
你好!
你可以自定义一个类包括几个属性解决这个问题
或可以数量和尺寸用逗号组成一个字符串 取出这个数据时候 用String..Split 方法分切一个数组- 已标记为答案 邹俊才Moderator 2009年8月26日 6:57
-
你好,如所述,你可以添加一个结构或者类来存储多个属性
public struct ProductInfo{public int PoductCount;public string ProductSize;public int ID;}
在添加的时候采用ht.Add(ID,productInfoInstance);
对于重复ID的情况,可以通过判断已有的Ht的key中式存在相同的,如果存在则将对应value的ProductCount增加1,否则直接添加
建议使用泛型更好些
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond- 已标记为答案 肖小勇Moderator 2009年8月26日 8:57
-
使用泛型可以简化你的代码,减少不必要的类型转换,你不应该回避不会的的知识,下面是一个使用泛型的示例,希望能给你帮助。------------------------------------------------------------------------------------------------------------------------------
using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class Default12 : System.Web.UI.Page { public class Product { /// <summary> /// 产品编号 /// </summary> public Int32 Id { get; set; } /// <summary> /// 产品数量 /// </summary> public Int32 Amount { get; set; } /// <summary> /// 产品尺码 /// </summary> public String Size { get; set; } public Product(Int32 fId, Int32 fAmount, String fSize) { this.Id = fId; this.Amount = fAmount; this.Size = fSize; } } /// <summary> /// 产品列表 /// </summary> public Dictionary<Int32, Product> Products { get { if (Session["Products"] == null) Session["Products"] = new Dictionary<Int32, Product>(); return (Dictionary<Int32, Product>)Session["Products"]; } } /// <summary> /// 添加或修改产品信息 /// </summary> /// <param name="fProductId">产品编号</param> /// <param name="fAmount">产品数量</param> /// <param name="fSize">产品尺码</param> public void AddOrModifyProduct(Int32 fProductId, Int32 fAmount, String fSize) { if (this.Products.ContainsKey(fProductId)) { // 如果已存在就更新数量和尺码 this.Products[fProductId].Amount = fAmount; this.Products[fProductId].Size = fSize; } else { // 如果不存在就添加 this.Products.Add(fProductId, new Product(fProductId, fAmount, fSize)); } } protected void Page_Load(object sender, EventArgs e) { // 添加三个产品信息 this.AddOrModifyProduct(1, 10, "X"); this.AddOrModifyProduct(2, 99, "XL"); this.AddOrModifyProduct(3, 50, "S"); // 修改产品编号为 1 的产品信息 this.AddOrModifyProduct(1, 19, "M"); // 遍历字典并显示在页面上 foreach (KeyValuePair<Int32, Product> vals in this.Products) { Response.Write(String.Format("Id:{0} Amount:{1} Size:{2}</br>" , vals.Key, vals.Value.Amount, vals.Value.Size)); } } }
知识改变命运,奋斗成就人生!- 已标记为答案 极度 2009年8月29日 13:38
全部回复
-
您好,两种思路供参考:
1、即使相同的产品名称,如果尺码不同则采用不同的产品ID,保证产品ID只描述一种特征的产品
2、将您认为关键的产品特性整合在一起设计成一个结构,用异或成员重写GetHashCode方法。将这个结构作为主键。- 已标记为答案 邹俊才Moderator 2009年8月26日 6:57
-
1 使用 Dictionary<TKey, TValue>, 定义一个类包含产品 Id, 数量,将这个类存入到 Value 中,(存到 Hashtable 的 Value 中也行,使用的时候再转换一次),不过建议 Dictionary 使用如下
public class Product { public Int32 ProductId { get; set; } public Int32 Amount { get; set; } public String Size { get; set; } public Product(Int32 fProductId, Int32 fAmount, String fSize) { this.ProductId = fProductId; this.Amount = fAmount; this.Size = fSize; } }
-- 使用Dictionary<String, Product> dic = new Dictionary<string, Product>(); dic.Add(1, new Product(1,1,"XL")); dic[1].Amount++;
2 使用 DataTable
知识改变命运,奋斗成就人生!- 已标记为答案 邹俊才Moderator 2009年8月26日 6:57
-
你好!
你可以自定义一个类包括几个属性解决这个问题
或可以数量和尺寸用逗号组成一个字符串 取出这个数据时候 用String..Split 方法分切一个数组- 已标记为答案 邹俊才Moderator 2009年8月26日 6:57
-
你好,如所述,你可以添加一个结构或者类来存储多个属性
public struct ProductInfo{public int PoductCount;public string ProductSize;public int ID;}
在添加的时候采用ht.Add(ID,productInfoInstance);
对于重复ID的情况,可以通过判断已有的Ht的key中式存在相同的,如果存在则将对应value的ProductCount增加1,否则直接添加
建议使用泛型更好些
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond- 已标记为答案 肖小勇Moderator 2009年8月26日 8:57
-
我是在页面的shop.aspx.cs 里写的代码,按照X.X.Y思路 暂时没用泛型.还不熟悉.用的hasttable 如下
public void Product(int id, int num, string chima)
{
id = sid;
num =Convert.ToInt32(DropCountt.SelectedValue);
chima = DropDownList1.SelectedValue;
}
#region 添加购物车
protected void ImgAddToCar_Click(object sender, ImageClickEventArgs e)
{
if (Session["UserName"] == null)
{
ClientScript.RegisterStartupScript(GetType(), "Start", clup.MessageBox("You have not login, you can not add shopping cart, please login", "signup.aspx"));
return;
}
else
{
int shopCarid = sid;
if (Session["ShopCar"] == null)
{
//如果用户没有分配购物车
ht = new Hashtable();//新生成一个
ht.Add(shopCarid, Product(shopCarid, Convert.ToInt32(DropCountt.SelectedValue), DropDownList1.SelectedValue)); //添加一个商品
Session.Add("ShopCar", ht); //分配给用户
}
else
{
ht = (Hashtable)Session["ShopCar"];//得到购物车的哈希表
if (ht.Contains(shopCarid)) //购物车中已有此商品,商品数量加1
{
int cout = Convert.ToInt32(ht[shopCarid].ToString());//得到该商品的数量ht[shopCarid] =cout+ Convert.ToString(DropCountt.SelectedValue);//商品数量加1
}
else
{
ht.Add(shopCarid, Product(shopCarid, Convert.ToInt32(DropCountt.SelectedValue), DropDownList1.SelectedValue));//如果没有此商品,则新添加一个项
}
}
foreach (DictionaryEntry each in ht)
{
string str = "编号" + shopCarid + "商品,型号为" + ht[shopCarid].ToString() + "</br>";
Response.Write(str);
}
// Response.Redirect("basket.aspx");
}
}
#endregion
运行提示有错误. roduct(shopCarid, Convert.ToInt32(DropCountt.SelectedValue), DropDownList1.SelectedValue)
不用羡慕----那,只是个传说! -
使用泛型可以简化你的代码,减少不必要的类型转换,你不应该回避不会的的知识,下面是一个使用泛型的示例,希望能给你帮助。------------------------------------------------------------------------------------------------------------------------------
using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class Default12 : System.Web.UI.Page { public class Product { /// <summary> /// 产品编号 /// </summary> public Int32 Id { get; set; } /// <summary> /// 产品数量 /// </summary> public Int32 Amount { get; set; } /// <summary> /// 产品尺码 /// </summary> public String Size { get; set; } public Product(Int32 fId, Int32 fAmount, String fSize) { this.Id = fId; this.Amount = fAmount; this.Size = fSize; } } /// <summary> /// 产品列表 /// </summary> public Dictionary<Int32, Product> Products { get { if (Session["Products"] == null) Session["Products"] = new Dictionary<Int32, Product>(); return (Dictionary<Int32, Product>)Session["Products"]; } } /// <summary> /// 添加或修改产品信息 /// </summary> /// <param name="fProductId">产品编号</param> /// <param name="fAmount">产品数量</param> /// <param name="fSize">产品尺码</param> public void AddOrModifyProduct(Int32 fProductId, Int32 fAmount, String fSize) { if (this.Products.ContainsKey(fProductId)) { // 如果已存在就更新数量和尺码 this.Products[fProductId].Amount = fAmount; this.Products[fProductId].Size = fSize; } else { // 如果不存在就添加 this.Products.Add(fProductId, new Product(fProductId, fAmount, fSize)); } } protected void Page_Load(object sender, EventArgs e) { // 添加三个产品信息 this.AddOrModifyProduct(1, 10, "X"); this.AddOrModifyProduct(2, 99, "XL"); this.AddOrModifyProduct(3, 50, "S"); // 修改产品编号为 1 的产品信息 this.AddOrModifyProduct(1, 19, "M"); // 遍历字典并显示在页面上 foreach (KeyValuePair<Int32, Product> vals in this.Products) { Response.Write(String.Format("Id:{0} Amount:{1} Size:{2}</br>" , vals.Key, vals.Value.Amount, vals.Value.Size)); } } }
知识改变命运,奋斗成就人生!- 已标记为答案 极度 2009年8月29日 13:38