Turn list to an array-VBForums
Results 1 to 5 of 5

Thread: Turn list to an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    7

    Question Turn list to an array

    Code:
    Private Sub CheckButton_Click(sender As Object, e As EventArgs) Handles CheckButton.Click
            For i = 1 To 58112          '58112 is the amount of words im checking against 
                If LCase(word) = LCase(arrwords(i)) Then
                    MsgBox("it is a word")
                    totalscore = totalscore + checkscore 
                    ListWM.Items.Add(word)   'words made so far
                End If
            Next
    
            TextScore.Text = totalscore
            ResetButton.PerformClick()
    end sub

    So i am making a boggle type game where you have 7 tiles and you have to make as many words as you can. I can check against the words in the dictionary just fine.
    I need help making an array of Words Made so far and making sure the user doesnt use the same word twice. I am kinda new to VB.Net and i dont know hot to convert a list to an array

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,578

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    7

    Re: Turn list to an array

    Quote Originally Posted by jmcilhinney View Post
    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!

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,578

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    279

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.