6 Replies - 624 Views - Last Post: 03 September 2013 - 12:48 PM Rate Topic: -----

#1 kat595  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-July 13

Why doesn't this code stop repeats? Visual Basic 2010

Posted 03 September 2013 - 03:56 AM

Why does the code below still repeat words when the game is being played?

 Dim lblArray As Label() = {lblOne, lblTwo, lblThree, lblFour}
Start:
        Dim WordChosen As String
        WordChosen = Nouns(Random.Next(16))

        If NewArray.Contains(WordChosen) Then
            GoTo Start
        Else
            Dim LabelChosen As Label
            LabelChosen = lblArray(Random.Next(4))
            LabelChosen.Text = WordChosen
            Nouns.Remove(WordChosen)
            NewArray.Add(WordChosen)
        End If



Here is the array:

Sub GetNoun()

        Dim Nouns As ArrayList = New ArrayList(16)
        Dim NewArray As ArrayList = New ArrayList(15)

        Nouns.Add("France")
        Nouns.Add("Bird")
        Nouns.Add("Doctor")
        Nouns.Add("City")
        Nouns.Add("Chicken")
        Nouns.Add("Banana")
        Nouns.Add("Food")
        Nouns.Add("Arm")
        Nouns.Add("Butter")
        Nouns.Add("Drum")
        Nouns.Add("Finger")
        Nouns.Add("Bomb")
        Nouns.Add("Brother")
        Nouns.Add("Hill")
        Nouns.Add("Cabbage")
        Nouns.Add("Crayon")
        Nouns.Add("Strawberry")
        Nouns.Add("Class")



The player clicks on the label which has the noun, so I have called the subroutine within the label_click:

 GetWords()
 GetNoun()



GetWords() just contains another ArrayList which contains words that aren't nouns, making it so the player has to choose the noun by clicking on it.

I am new to programming and the code works apart from words still get repeated, which I would prefer not to happen. The idea I had was copying the WordChosen to a new ArrayList (NewArray) and then if the WordChosen is within that array then I want the computer to not pick it again, as I hope my code above shows this.

Any help is appreciated and have tried really hard myself to figure it out, but would like to ask as it isn't going well!

Is This A Good Question/Topic? 0
  • +

Replies To: Why doesn't this code stop repeats? Visual Basic 2010

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Why doesn't this code stop repeats? Visual Basic 2010

Posted 03 September 2013 - 04:10 AM

Quote

Any help is appreciated and have tried really hard myself to figure it out, but would like to ask as it isn't going well!

I sincerely wish you learn how to debug your program, that way you'll learn what is going on in your application and where the duplication takes place.
Was This Post Helpful? 0
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Why doesn't this code stop repeats? Visual Basic 2010

Posted 03 September 2013 - 05:31 AM

Place a breakpoint on line 4 ( WordChosen = Nouns(Random.Next(16))), run your program, and when it stops, single-step until you see the problem. It won't take very long to see it.

If you don't know how to do this, see our excellent Debugging tutorials, here, and here.

This post has been edited by lar3ry: 03 September 2013 - 05:32 AM

Was This Post Helpful? 1
  • +
  • -

#4 kat595  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-July 13

Re: Why doesn't this code stop repeats? Visual Basic 2010

Posted 03 September 2013 - 09:16 AM

I have done what you said, using the breakpoints and stepping, but still stuck with the coding part. I find that the words don't stay in the NewArray when a new call is sent from label_click event, therefore, the nouns get repeated because there isn't anything in the NewArray. Doesn't .Add keep the item in the array?

I am not sure if this is even making any sense? I don't mind the OtherWords() repeating, just not the Nouns().

:(
Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Why doesn't this code stop repeats? Visual Basic 2010

Posted 03 September 2013 - 10:35 AM

Your Subroutine GetNouns() declares ArrayLists Called Nouns and NewArray, and adds a number of nouns Nouns(). If your routine at Start: is within another subroutine, you will not be able to see Nouns() or NewArray() from it.

You might get better help, without a lot of guessing, if you supply us with more of the code.

This post has been edited by andrewsw: 03 September 2013 - 10:41 AM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

#6 kat595  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-July 13

Re: Why doesn't this code stop repeats? Visual Basic 2010

Posted 03 September 2013 - 10:58 AM

I figured out that the declaration of the array should be global, not local. That was all! I found it out using the debugging tools you suggested :) Thank-you!
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Why doesn't this code stop repeats? Visual Basic 2010

Posted 03 September 2013 - 12:48 PM

View Postkat595, on 03 September 2013 - 11:58 AM, said:

I figured out that the declaration of the array should be global, not local. That was all! I found it out using the debugging tools you suggested :)/> Thank-you!

You're welcome.

Here's another tip. Read the link in my signature about Option Strict. Debugging skills and Option Strict are two things that will save you endless hours in programming.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1