0

I can't work out what's causing it. The error report says "Object variable or With block variable not set." for the line "allSlotLabels(i).Image = imgCherries". This line is no different from the other, so I guess it's just the error it picked up first after randomly generating the number. Any help at all would be appreciated, I'm completely stuck.

Public Class frmSlotMachine

' Declare all variables needed
Dim startingCoins As Integer = 5
Dim coins As Integer = startingCoins + 1
Dim numbersGenerated As Integer = 20
Dim spinStatus As String = "Start"
Dim held1 As Boolean = False
Dim held2 As Boolean = False
Dim held3 As Boolean = False
Dim slot1Name, slot2Name, slot3Name As String
Dim slot1Value, slot2Value, slot3Value As Integer
' Assign resources to variables
Dim imgBanana As Image = My.Resources.banana
Dim imgOrange As Image = My.Resources.orange
Dim imgSeven As Image = My.Resources.seven
Dim imgCherries As Image = My.Resources.cherries
Dim imgBatman As Image = My.Resources.batman
Dim imgCross As Image = My.Resources.cross
' Declare arrays
Dim allHelds() As Boolean = {held1, held2, held3}
Dim allSlotValues() As Integer = {slot1Value, slot2Value, slot3Value}
Dim allSlotNames() As String = {slot1Name, slot2Name, slot3Name}
Dim allSlotLabels() As Object = {lblSlot1, lblSlot2, lblSlot3}

Private Sub btnSpin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpin.Click


    ' Trying a for loop to randomise numbers and assign images, if hold is off
    For i = 1 To 3
        If Not allHelds(i) Then
            allSlotValues(i) = Int(Rnd() * numbersGenerated + 0.5)
            Select Case allSlotValues(i)
                Case 0 To 5
                    allSlotLabels(i).Image = imgBanana
                    allSlotNames(i) = "Banana"
                Case 6 To 11
                    allSlotLabels(i).Image = imgOrange
                    allSlotNames(i) = "Orange"
                Case 12 To 16
                    allSlotLabels(i).Image = imgCherries
                    allSlotNames(i) = "Cherries"
                Case 17 To 19
                    allSlotLabels(i).Image = imgSeven
                    allSlotNames(i) = "Seven"
                Case 20
                    allSlotLabels(i).Image = imgBatman
                    allSlotNames(i) = "Batman"
                Case Else
                    allSlotLabels(i).Text = "Error. Current slot value = " & allSlotValues(i)
            End Select
        End If
    Next
2
  • 1
    What is the value of i at the time of the exception? Where and to what are lblSlot1, lblSlot2 and lblSlot3 initialized before they are stored in the array? Commented Nov 9, 2011 at 20:03
  • I'm sorry, but I'm so new to this. How do I display the values of my variables and step in Visual Basic Studio? And I don't know where the labels are initialised, I just dragged and dropped them onto my form. Commented Nov 9, 2011 at 20:15

2 Answers 2

3

How about: For i = 0 To 2. Indexes start with 0, not with 1.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, that probably solved a future problem. But the primary problem remains. There's also probably a good few other bugs in the program I haven't found yet, but I'll work them out later.
@SiliconCelery - it solves your primary problem. On the line where it fails i will be 3.
What is the value of i when it happens and is it always for cherries, since you have rnd, will it happen for other values or rnd? Did you set those labels to something, I cannot see that in your code... But your code is rather, well, interesting, and you might want to redesign the whole thing, by using structures for instance.
I still get exactly the same error report, ChrisF. I'll try to change the values and see what I get, zmilojko. And I know my code is dreadfully and inefficient, but I'm only just learning it. And a few arrays is an improvement on my 20 - something if statements I had before.
And I still ask the same question: have you set the labels to something or are they null?
|
3

change the assignment of slots as follows:

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    allSlotLabels(0) = lblslot1
    allSlotLabels(1) = lblslot2
    allSlotLabels(2) = lblslot3
End Sub

and the loop to

 For i = 0 To 2

2 Comments

Yes, this is the answer. Beat me by a minute.
The reason behind this is the statement Dim allSlotLabels ... is being executed before the labels are created. By the time you reach the Form.Load event, all of the controls are created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.