2

セッションに入れた辞書があり、ボタンをクリックするたびに何らかの操作を実行する必要があります。

itemColl = new Dictionary<int, int>();

セッション変数で保持しているキーを検索したいのですが、キーが存在する場合は、対応するキーの値を 1 増やしたいのですが、どうすればこれを達成できますか。

私は次のように試しています:

if (Session["CurrCatId"] != null)
{
    CurrCatId = (int)(Session["CurrCatId"]);
    // this is the first time, next time i will fetch from session
    // and want to search the currcatid and increase the value from
    // corresponding key by 1.
    itemColl = new Dictionary<int, int>();
    itemColl.Add(CurrCatId, 1);
    Session["itemColl"] = itemColl;                            
}
4

3 に答える 3

1

編集:申し訳ありませんが、私は以前に質問を理解していませんでした。次のように、単にそのキーを使用しようとします。

try
{
    if (condition)
        itemColl[i]++;
}
catch
{
    // ..snip
}

a を使用してtry-catch、何らかの理由でキーが存在しない場合にエラーを処理できるようにします。

于 2013-05-15T12:41:44.000 に答える