2011-03-20 30 views
0

asp.netウィザードのフォームデータをセッション配列オブジェクトに追加しようとしています。オブジェクトには、質問ごとにユーザーが選択した質問番号と質問回答を格納する必要があります。私は、多次元配列を使用して質問番号と関連する回答を格納しようとしています。私は、ハッシュテーブルまたは辞書のソリューションを使用することもできます。c#セッション多次元配列

これは私がこれまで持っているコードです:

string[,] strQandA = new string[10, 2] {{"1", Q1.SelectedValue}, {"2", Q2.SelectedValue}, {"3", Q3.SelectedValue}, {"4", Q4.SelectedValue}, {"5", Q5.SelectedValue}, {"6", Q6.SelectedValue}, {"7", Q7.SelectedValue}, {"8", Q8.SelectedValue}, {"9", Q9.SelectedValue}, {"10", Q10.SelectedValue}}; 
Session["mySession"] = strQandA; 

は、この正しいですか?どんな助けもありがとう。ありがとう

+1

「これは正しいですか? == "試してもらえましたか?" –

+0

私はResponse.Write(Session ["mySession"])を試みました。 System.String [、]を返します。 – multiv123

+1

これは、 'Write'を呼び出すと、渡されたオブジェクトのToString()を呼び出します。オーバーロードのないref型で 'ToString()'を呼び出すと、デフォルトで型が書き出されます。値を取得するには、配列にインデックスを付ける必要があります。 –

答えて

1

あなたのニーズに合ったより良い練習は、質問番号をキーとして、値として選択した値を値として持つ辞書です。次のようなものがあります。

DropDownList[] arrDDL = new DropDownList[] { Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10 }; 
Dictionary<int, string> strQandA = new Dictionary<int, string>(); 
for (int i = 0; i < arrDDL.Length; i++) 
    strQandA.Add(i + 1, arrDDL[i].SelectedValue); 
Session["mySession"] = strQandA; 

3番目の質問の回答:

(Session["mySession"] as Dictionary<int, string>)[3] 
0

あなたは、オブジェクトの上にResponse.Writeを呼び出すToString()を呼び出す必要があり、デフォルトの実装を返します。

Dictionary<string, YourType> strQandA = new Dictionary<string, YourType>(); 
Session["mySession"] = strQandA; 

Dictionary<string, YourType> mySession = (Dictionary<string, YourType>)Session["mySession"];