vb.net - How to make a live score counter for Lucky 7 (vb game) -


i have made simple game, lucky 7 on visual basic using vb code. score counter doesn't work properly, example if win game once (get 7 in 1 of 3 slots), 10 points , score label changes 10. if continue pressing spin button , win again, score label still stays on number 10, , not change 20.

here code spin button wrote:

private sub button1_click(sender system.object, e system.eventargs) handles button1.click      dim rand = new random     dim slots = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}     dim score = 0     = 0 2         slots(i) = rand.next(10)     next      label1.text = (slots(0).tostring)     label2.text = (slots(1).tostring)     label3.text = (slots(2).tostring)      if slots(0) = 7 or slots(1) = 7 or slots(2) = 7         score = score + 10           label4.text = (score.tostring)         picturebox1.visible = true      else         picturebox1.visible = false     end if  end sub 

do need add while loop or similar make score change many times win game?

you need move variable declaration @ class level.

at moment, create when click on button. therefore, each time click, delete score variable , create again.

move

dim score = 0 

line follows:

'assuming form called form1 public class form1 inherits form  dim score = 0       private sub button1_click(sender system.object, e system.eventargs) handles button1.click      'your current code      end sub   end class 

and problem solved.

you should read documentation scopes.

an extract little mistake :

if declare variable within procedure, outside of if statement, scope until end sub or end function. lifetime of variable until procedures ends.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -