I have this application where i have to insert the same string
in a textbox
as the one in the array
. The strings
come by in a label, for every answer it will go to a random word/string that is in the array.
My question is: Is there any way to delete
or disable
a word/string in the array
so I don't get any duplicates?
string[] arrProvincies = File.ReadAllLines(@"provincies.txt");
int counter = 0;
string Array = "";
string Array = arrProvincies[counter].ToString();
lblProvincie.Text = Array;
counter = rnd.Next(0, 12);
2 Answers 2
You can use a Queue:
Queue<string> arrProvincies = new Queue<string>(File.ReadAllLines(@"provincies.txt"));
And use it with lblProvincie.Text = arrProvincies.Dequeue();
Thus you'll be sure to have no duplicate since your remove the entry when you use it.
If you need shuffling:
Queue<string> arrProvincies = new Queue<string>(File.ReadAllLines(@"provincies.txt").OrderBy(o => Guid.NewGuid()));
7 Comments
arrProcincies
with mineGuid.NewGuid()
instead of new Guid()
, as that will give you random Guids and not just Guid.Empty
string[] arrProvincies = File.ReadAllLines(@"provincies.txt");
var randomProvincies = arrProvincies.Distinct().OrderBy(i => Guid.NewGuid());
randomProvincies would be the distinct values ordered randomly (I couldn't understand the part where it says you would insert the same string in a textbox).
var provinces=arrProvincies.Distinct()
to get distinct entriesPrevent duplicates
is not the same asrandom selection without repetition
. What do you actually want?