Re: Turn list to an array
There doesn't seem to be a reason to create an array if you already have a List. That said, if you use a HashSet instead then that solves all your problems. A HashSet doesn't allow duplicates and it's Add method will return False if you try. So, create a HashSet(Of String) and the call its Add method to add each word. If that returns False then the word was not added because it's already in the list.
Re: Turn list to an array
Quote:
Originally Posted by
jmcilhinney
There doesn't seem to be a reason to create an array if you already have a List. That said, if you use a HashSet instead then that solves all your problems. A HashSet doesn't allow duplicates and it's Add method will return False if you try. So, create a HashSet(Of String) and the call its Add method to add each word. If that returns False then the word was not added because it's already in the list.
i have never used a hash set before but ill go look it up! thanks man!
Re: Turn list to an array
Using a HashSet is almost exactly the same as using a List. The only real difference is what I've already mentioned, so there's really very little to learn. It might be worth your while to peruse the System.Collections namespace and its children to see what sorts of collections are available. There's a number that provide useful functionality for particular situations. Just note that most of what you will use these days are in System.Collections.Generic or, for deriving your own types, System.Collections.ObjectModel.
Re: Turn list to an array
This is how I would have done it (before hearing about HashSet).
Code:
dim ListOfUsedWords as New List(Of String)
Private Sub CheckIfWordHasAlreadyBeenUsed(Word as string)
if ListOfUsedWords.Contains(Word) then
msgbox("WORD ALREADY USED")
else
ListOfUsedWords.add(Word)
end if
Didn't know about the existance of HashSet, out of curiousity I read up a bit, turns out they're actually considerably faster than List<of T> too.
Because a HashSet is constrained to contain only unique entries, the internal structure is optimised for searching (compared with a list) - it is considerably faster
A HashSet<T> is a class designed to give you O(1) lookup for containment
Interesting.