2

I have this working code for my dictionary:

dict = new Dictionary<string, string>();

using (StreamReader read = new StreamReader("dictionaryfile.csv"))
{
      string line;
      while ((line = read.ReadLine()) != null)
      {
          string[] splitword = line.Split(',');  
          dict.Add(splitword[0], splitword[1]);
      }
}

I've added a button to my Windows form, and how would I assign a random entry from my dictionary to show in a message box from the button click?

Omar
  • 16,329
  • 10
  • 48
  • 66
user1353517
  • 300
  • 3
  • 10
  • 28
  • ...Nothing as I don't know how to do it, that's why i'm asking for help how to do it – user1353517 Apr 29 '12 at 19:44
  • 2
    Are you seriously telling me you didn't even google "random c#"? – Oded Apr 29 '12 at 19:44
  • Is there any reason not to add entries to a `List>` at the same time as the dictionary? – Jon Skeet Apr 29 '12 at 19:45
  • Yes I did, but I didn't find anything I understood. – user1353517 Apr 29 '12 at 19:47
  • Then you really need to explain what you don't understand - there are too many options for us to consider. What _exactly_ are you having problems with? Please be as detailed as possible. – Oded Apr 29 '12 at 19:48
  • I don't know how I'd make a random entry from my dictionary show in a message box from my button click. – user1353517 Apr 29 '12 at 19:51
  • Why the downvotes?! because I couldn't understand anything from a google result? – user1353517 Apr 29 '12 at 20:03
  • So is the problem that you don't know how to display a message box or that you don't know how to get a random entry from the dictionary... this is what people don't understand I think. No one knows where your problem is. I didn't downvote (yet) but I'm guessing this is why people are voting down. – Pete Apr 29 '12 at 20:04
  • I just don't know how to get a random entry from the dictionary. I guess I thought I made that clear enough in the OP but I guess not – user1353517 Apr 29 '12 at 20:06

1 Answers1

5

You're probably looking for the Random class and an OrderedDictionary:

var dict = new System.Collections.Specialized.OrderedDictionary(); 
dict.Add("key1", "value1");
dict.Add("key2", "value2");
dict.Add("key3", "value3");
dict.Add("key4", "value4");
// get a random value 
var rnd = new Random();
var randomValue = (String)dict[rnd.Next(0, dict.Count)];

Edit: Here's an approach using a Dictionary<String,String> and the ElementAt method:

var rnd = new Random();
var randomEntry = dict.ElementAt(rnd.Next(0, dict.Count));
String randomKey = randomEntry.Key;
String randomValue = randomEntry.Value;

Note that you should not create the random instance in a method, you should either pass it as parameter or use a member variable: https://stackoverflow.com/a/768001/284240

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • that's sort of what I'm looking for, but I don't understand why you've put the dict.Add code there since i'm not adding anything. I'm just looking to display a random entry on a button click – user1353517 Apr 29 '12 at 19:58
  • Tim's example was a code snippet you can copy and paste into a function to run on its own - for your exact problem you essentially need his last 2 lines of code. If this is for code that is going to be used for more than an assignment/prototype/learning, I strongly recommend you read up on the Random class, as there are a few aspects of random number generations that developers need to know about. – Stefan Mohr Apr 29 '12 at 20:04
  • @user1353517: Edited my answer to provide also a way for a `Dictionary`. My code is sample code. Of course you need to reference the "global" dictionary that is populated from form-load(or whencever) in your button's click event handler. – Tim Schmelter Apr 29 '12 at 20:10
  • @L.B Why must I be joking? I'm new to c# and tbh you aren't really helping anything! Thanks Tim, I think I get it now – user1353517 Apr 29 '12 at 20:13