views:

5012

answers:

5

I have some code which populates a hashtable with a question as the key and an arraylist of answers as the value.

I want to then print out these values from the hashtable so that it displays the question and corresponding solutions for each individual question in the hashtable.

I know I have done something totally stupid with the foreach loop to printout the hashtable contents, but i've been coding for a good few hours straight and I can't think of the logic to printout my nested arraylist.

Help appreciated greatly.

Here is the code:

//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());
            }
        }
A: 

Shouldn't this:

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

be this?

foreach(var obj in sourceList.Values)
    Debug.WriteLine(obj);
Nathan Ridley
+1  A: 

Try this

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

            }
Gary
+1  A: 

Minor tweaks

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
}
Gishu
+4  A: 

As you are using LINQ you are obviously not constrained to framework 1.1, so you should not be using the HashTable and ArrayList classes. You should use the strictly typed generic Dictionary and List classes instead.

You don't need a class to keep the question and answers in as you have the Dictionary. The class would only be an extra container with no real purpose.

//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);
   }
}

If you need the class for some other reason, that could look like below.

(Note that the question string is both referenced in the class and used as key in the dictionary, but the dictionary key isn't really used for anything in this code.)

//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);
   }
}
Guffa
Fantastic answer thankyou, I guess I really shouldn't have bothered with non-type safe solutions in the first place.Thankyou
Goober
A: 

First, a strongly typed generic collection would make it easier. Let's start by defining an alias for the strongly typed collection:

using MyHash = System.Collections.Generic.Dictionary<string,
    System.Collections.Generic.List<string>>;

From now on, MyHash means the same as the lengthy generic definition. Now you can declare the hashtable member like:

static MyHash sourceList = new MyHash();

And iterate over it like:

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

Hope this is useful.

Andomar