VB.net array help needed!

Associate
Joined
12 Jun 2003
Posts
2,033
Location
Either Tonbridge or Biggin Hill
Hi all,
In VB.net how do I randomise the contents of a 2-dimensional string array?

For example, I have an array- arrNames(). Into this array is loaded a series of names (read from a text file).

I want to randomise the order the names are stored in the array and then finally I want to loop through the array to populate two list boxes. So the even elements are added to lstTeam1 and the odd elements are added to lstTeam2.

Can you help? :)
 
The quickest way i can think of to do this is to load the names into an ArrayList, inserting them at random. So you load the first name into the arraylist, and then you call the insert method on the arraylist and use a random number to specify the index at which to insert it, making sure that number isn't bigger than the size of the arraylist. Then you can read the arraylist in numerical order, which should be the names in a random order.

Hope that makes some sort of sense...

*edit* alternatively, instead of having the names as strings, you could have them as a class inherited from string but with the compare method overloaded to give a random answer when entry1 is compared with entry2. Problem with that is that your sort function might take a long time and may never finish, as when you compare entry1 with entry2, you probably won't get the same answer more than once. Might be interesting to try though.

Again, hope that made some vague sort of sense :)
 
Last edited:
Back
Top Bottom