0

Can anyone please help me to add number of strings to a hash table. Actually i am trying to make a "hangman game". I want to store the answers in a hash table but there's something wrong i my code <sorry!! I can't provide the code>.

Please suggest me a way to store the stringsin a hash table. If possible suggest a way to look for the right ansnwer in the hash table.

Thanks in advance.

2
Contributors
1
Reply
15
Views
3 Years
Discussion Span
Last Post by chriswelborn
0
using System;
using System.Collections.Generic;

// Creating and adding answers.
Dictionary<String, String> answers = new Dictionary<String, String> () {
    {"mykey1", "myanswer1"},
    {"mykey2", "myanswer2"}
};

// Adding on the fly.
answers.Add ("mykey3", "myanswer3");

// Retrieving answers.
String trykey = "mykey2";
String answer;
Boolean retrieved = answers.TryGetValue (trykey, out answer);
if (retrieved) {
    Console.WriteLine (
        String.Format ("Retrieved: {0}", answer));
} else {
    Console.WriteLine (
        String.Format ("Invalid key! Can't find an answer for: {0}", trykey));
}

I have no idea what your keys are supposed to be, so I chose String.

Edited by chriswelborn: Formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.