Question need some help. can't understand

boneh

New member
Joined
Apr 17, 2010
Messages
3
Programming Experience
Beginner
Hi guys,
I got a question about vb.net 2005.
I saw this code somewhere but don't know how he knew this. I'll show you.
I mark the thing in red
I don't understand how he knew the (10,2) can someone help me here plz?
Because if I take something else then (10,2) I'll get an error on this one. I marked it in Blue



Public Class frmMemory
Dim Choice As Integer, Picked(2) As Integer
Dim Behind(16) As Integer
Dim Guesses As Integer, Remaining As Integer
Dim Boxes(16) As PictureBox
Dim Choices(8) As PictureBox
Dim MyRandom As New Random


Private Sub picHidden_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picHidden_01.Click
Dim PictureClicked As PictureBox
Dim Index As Integer
PictureClicked = CType(sender, PictureBox)
Index = CInt(Val(Mid(PictureClicked.Name, 10, 2)))
'If trying to pick same box, picking already selected box
'or trying pick when not playing, exit
If (Choice = 2 And Index = Picked(1)) Or Behind(Index) = -1 Or btnNew.Enabled Then
Exit Sub
End If
'Display selected picture
Boxes(Index).Image = Choices(Behind(Index)).Image
Boxes(Index).Refresh()
If Choice = 1 Then
Picked(1) = Index
Choice = 2
Exit Sub
End If
Guesses = Guesses + 1
lblGuesses.Text = Format(Guesses, "0")
Picked(2) = Index
If Behind(Picked(1)) = Behind(Picked(2)) Then
'If match, play sound
Behind(Picked(1)) = -1
Behind(Picked(2)) = -1
Remaining = Remaining - 1
Else
'If no match, blank picture, restore backs
Boxes(Picked(1)).Image = picBack.Image
Boxes(Picked(2)).Image = picBack.Image
End If
Choice = 1
If Remaining = 0 Then
btnExit.PerformClick()
btnNew.Focus()
End If
End Sub

Private Sub frmMemory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim I As Integer
'load images
picBack.Image = My.Resources.Black
picChoice1.Image = My.Resources.nr1
picChoice2.Image = My.Resources.nr2
picChoice3.Image = My.Resources.nr3
picChoice4.Image = My.Resources.nr4
picChoice5.Image = My.Resources.nr5
picChoice6.Image = My.Resources.nr6
picChoice7.Image = My.Resources.nr7
picChoice8.Image = My.Resources.nr8
'form arrays of controls with images
Boxes(1) = picHidden_01
Boxes(2) = picHidden_02
Boxes(3) = picHidden_03
Boxes(4) = picHidden_04
Boxes(5) = picHidden_05
Boxes(6) = picHidden_06
Boxes(7) = picHidden_07
Boxes(8) = picHidden_08
Boxes(9) = picHidden_09
Boxes(10) = picHidden_10
Boxes(11) = picHidden_11
Boxes(12) = picHidden_12
Boxes(13) = picHidden_13
Boxes(14) = picHidden_14
Boxes(15) = picHidden_15
Boxes(16) = picHidden_16
Choices(1) = picChoice1
Choices(2) = picChoice2
Choices(3) = picChoice3
Choices(4) = picChoice4
Choices(5) = picChoice5
Choices(6) = picChoice6
Choices(7) = picChoice7
Choices(8) = picChoice8

'set click events of picture boxes
For I = 1 To 16
AddHandler Boxes(I).Click, AddressOf Me.picHidden_Click
Next
btnNew.PerformClick()
End Sub

Private Sub btnNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNew.Click
Dim I As Integer
Guesses = 0 : Remaining = 8
lblGuesses.Text = "0"
'Randomly sort 16 integers using Shuffle routine
'Behind contains indexes (1-8) for hidden pictures
Call Shuffle(16, Behind)
For I = 1 To 16
'reset image
Boxes(I).Image = picBack.Image
If Behind(I) > 8 Then
Behind(I) = Behind(I) - 8
End If
Next I
Choice = 1
btnNew.Enabled = False
btnExit.Text = "&Stop"
End Sub
Public Sub Shuffle(ByVal NumberOfItems As Integer, ByVal NumberList() As Integer)
'Shuffles integers from 1 to NumberOfItems
'Procedure level variables
Dim TempValue As Integer
Dim LoopCounter As Integer
Dim ItemPicked As Integer
Dim Remaining As Integer
'Initialize array
For LoopCounter = 1 To NumberOfItems
NumberList(LoopCounter) = LoopCounter
Next LoopCounter
'Work through Remaining values
'Start at NumberOfItems and swap one value
'at each For/Next loop step
'After each step, Remaining is decreased by 1
For Remaining = NumberOfItems To 2 Step -1
'Pick item at random
ItemPicked = CInt(Rnd() * Remaining - 0.5) + 1
'Swap picked item with bottom item
TempValue = NumberList(Remaining)
NumberList(Remaining) = NumberList(ItemPicked)
NumberList(ItemPicked) = TempValue
Next Remaining
End Sub
End Class



thx for helping.
 
Couple of things: When posting code use the [code] [/code] tags so it's at least readable. Also explaining what you're trying to do here would be helpful, I can see possibly why this line errors:
Index = CInt(Val(Mid(PictureClicked.Name, 10, 2)))

but it's only a guess since I don't know what type of a name the PictureBox would even have.
 
hi,
it's hard to explain. But I'm trying to make a memory game 4x4
And the problem is when I click on a picturebox 1 of the 16 I get an error of NullReferenceException was unhandled.
The pictures are called: picHidden_01 / picHidden_02 etc...
Then there are 8 picturebox with name picChoice1 / picChoice2 etc..
these picChoices are the diffrent pictures.
So I'm trying to click on 2 pictures and when they are the same they can just stand there. But when they are wrong they have to be turned over again. But don't know how to do this.. Some advise?

thx
 
Try
VB.NET:
[COLOR=black]Index = CInt(Val(Mid(PictureClicked.Name, 11, 2)))[/COLOR]
This will get the number part of the name from the 11th character, 2 characters long.

For example if the pictureClicked.Name= "picHidden_16" then the Index value will be 16
 
Personally I would just do this:
VB.NET:
If Integer.TryParse(PictureClicked.Name.SubString(10I), Index) Then
    'Index has the # in it now
End If
 
Back
Top