2009-05-02 6 views
2

ハッシュテーブルに質問としてキーを、回答のarraylistに値を設定するコードがあります。C#Foreachループハッシュテーブル問題

これらの値をハッシュテーブルから印刷して、個々の質問ごとに質問とそれに対応する解をハッシュテーブルに表示したいとします。

ハッシュテーブルの内容を出力するためにforeachループを使って何かをやってしまったことは知っていますが、数時間もかけてコードを書いていて、論理を入れ子にしたarraylistを考えることはできません。

非常に感謝しています。ここで

はコードです:

//Hashtable Declaration 
static Hashtable sourceList = new Hashtable();  

//Class For Storing Question Information 
public class QuestionAnswerClass 
{ 
    public string simonQuestion; 
    public ArrayList simonAnswer = new ArrayList(); 
} 

//Foreach loop which populates a hashtable with results from 
//a linq query that i need to print out. 
foreach (var v in linqQueryResult) 
     { 
      Debug.WriteLine(v.question); 
      newques.simonQuestion = v.question; 
      //Debug.WriteLine(v.qtype); 
      //newques.simonQType = v.qtype; 

      foreach (var s in v.solution) 
      { 
       Debug.WriteLine(s.Answer); 
       newques.simonAnswer.Add(s.Answer); 
      } 
     }   

     sourceList.Add(qTextInput,newques); 

//foreach loop to print out contents of hashtable 
foreach (string key in sourceList.Keys) 
     { 
      foreach(string value in sourceList.Values) 
      { 
       Debug.WriteLine(key); 
       Debug.WriteLine(sourceList.Values.ToString()); 
      } 
     } 
+1

でより多くの検索は、のハッシュテーブルのキーとハッシュテーブルのデータ。確認して、近くに投票してください。 – dirkgently

答えて

10

をお試しください。代わりに厳密に型指定された汎用DictionaryListクラスを使用する必要があります。

あなたはDictionaryを持っているので、質問と回答を保持するクラスは必要ありません。クラスは実際の目的のない余分なコンテナだけです。

//Dictionary declaration 
static Dictionary<string, List<string>> sourceList = new Dictionary<string, List<string>>(); 

//Foreach loop which populates a Dictionary with results from 
//a linq query that i need to print out. 
foreach (var v in linqQueryResult) { 
    List<string> answers = v.solution.Select(s => s.Answer).ToList(); 
    sourceList.Add(v.question, answers); 
}   

//foreach loop to print out contents of Dictionary 
foreach (KeyValuePair<string, List<string>> item in sourceList) { 
    Debug.WriteLine(item.Key); 
    foreach(string answer in item.Value) { 
     Debug.WriteLine(answer); 
    } 
} 

その他の理由でクラスが必要な場合は、次のようになります。

(質問文字列が両方のクラスで参照し、辞書でキーとして使用されるが、辞書のキーは本当にこのコードでは何のために使用されていないことに注意してください。)

//Class For Storing Question Information 
public class QuestionAnswers { 

    public string Question { get; private set; } 
    public List<string> Answers { get; private set; } 

    public QuestionAnswers(string question, IEnumerable<string> answers) { 
     Question = question; 
     Answers = new List<string>(answers); 
    } 

} 

//Dictionary declaration 
static Dictionary<string, QuestionAnswers> sourceList = new Dictionary<string, QuestionAnswers>(); 

//Foreach loop which populates a Dictionary with results from 
//a linq query that i need to print out. 
foreach (var v in linqQueryResult) { 
    QuestionAnswers qa = new QuestionAnswers(v.question, v.solution.Select(s => s.Answer)); 
    sourceList.Add(qa.Question, qa); 
}   

//foreach loop to print out contents of Dictionary 
foreach (QustionAnswers qa in sourceList.Values) { 
    Debug.WriteLine(qa.Question); 
    foreach(string answer in qa.Answers) { 
     Debug.WriteLine(answer); 
    } 
} 
+0

ファンタスティックな返信ありがとうございました。場所。 ありがとうございます – Goober

0

は、このべきではない:

Debug.WriteLine(sourceList.Values.ToString()); 

はこのこと?あなたは明らかに、フレームワーク1.1に制約されないLINQを使用しているとして、あなたはHashTableArrayListクラスを使用するべきではありませんので

foreach(var obj in sourceList.Values) 
    Debug.WriteLine(obj); 
2

、この

foreach (DictionaryEntry entry in sourceList) 
      { 
       Debug.WriteLine(entry.Key); 
       foreach (object item in (ArrayList)entry.Value) 
       { 
        Debug.WriteLine(item.ToString()); 
       } 

      } 
1

マイナーひねり

foreach (string key in sourceList.Keys) 
{ 
    Console.WriteLine(key); 
    foreach(string value in sourceList[key]) 
    { 
    Console.WriteLine("\t{0}", value); // tab in answers one level 
    } 
    Console.WriteLine(); // separator between each set of q-n-a 
} 
+0

エラー1: 'オブジェクト'に 'GetEnumerator'のパブリック定義が含まれていないため、foreach文は 'オブジェクト'型の変数では機能しません。 – Jack

0

は、まず、強く型付けされたジェネリックコレクションはそれをより簡単にします。まず、厳密に型指定されたコレクションのエイリアスを定義してみましょう。これからは、MyHashは長年の一般的な定義と同じ意味です。今、あなたのようなハッシュテーブルメンバーを宣言することができます。

static MyHash sourceList = new MyHash(); 

そして、それを反復処理するように:これは便利です

foreach (var pair in sourceList) 
{ 
    var question = pair.Value; 
    Console.WriteLine(question); 
    foreach (var answer in pair.Value) 
     Console.WriteLine(" " + answer); 
} 

希望を。