0

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);
Thomas Ayoub
29.6k16 gold badges98 silver badges149 bronze badges
asked Sep 12, 2016 at 12:55
4
  • 1
    Just call var provinces=arrProvincies.Distinct() to get distinct entries Commented Sep 12, 2016 at 12:57
  • 3
    Prevent duplicates is not the same as random selection without repetition. What do you actually want? Commented Sep 12, 2016 at 12:59
  • I don't want to have the same string more then once. Commented Sep 12, 2016 at 13:02
  • 1
    @Tuur you are looking for shuffling, generating a random order from the original list. Commented Sep 12, 2016 at 13:03

2 Answers 2

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()));
answered Sep 12, 2016 at 12:57
Sign up to request clarification or add additional context in comments.

7 Comments

The OP is trying to retrieve entries randomly. The lines should be shuffled before loading in the queue
@PanagiotisKanavos true, added info.
@Tuur replace your declaration of arrProcincies with mine
Only problem is, it will go by the same order everytime, it might be shuffled, but that stays the same everytime
I think you want Guid.NewGuid() instead of new Guid(), as that will give you random Guids and not just Guid.Empty
|
1
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).

answered Sep 12, 2016 at 13:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.