12 Replies - 502 Views - Last Post: 17 October 2013 - 10:07 AM Rate Topic: -----

#1 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Slot Machine, Error displayed.

Posted 17 October 2013 - 07:40 AM

Hi, I just started working on a new school Challenge called a Slot machine and the Objective is to generate random numbers in 3 different labels. ( lbl1, lbl2, lbl3 ). And If the labels all match the same number, then a 4th label (lblWonOrLost) should display "You Win" or else "You Lose" My problem that I am encountering is that before the program even starts I get an error displaying this photo:Attached Image Here is my code that I have typed so far: Attached Image

Help GReatly Appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Slot Machine, Error displayed.

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8897
  • View blog
  • Posts: 33,364
  • Joined: 12-June 08

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 07:51 AM

You can pretty much determine what you broke by just using a break point. Put it at the top of your code.. step through it.. notice how randN is declared but never instantiated... then you go off trying to use an un-instantiated variable.
Was This Post Helpful? 0
  • +
  • -

#3 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 07:59 AM

View Postmodi123_1, on 17 October 2013 - 07:51 AM, said:

You can pretty much determine what you broke by just using a break point. Put it at the top of your code.. step through it.. notice how randN is declared but never instantiated... then you go off trying to use an un-instantiated variable.

I am kind of new at this. Could you please elaborate a bit more? Can't understand the new terms.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8897
  • View blog
  • Posts: 33,364
  • Joined: 12-June 08

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 08:00 AM

You named a variable - aka declared it.

You never created the memory for it - aka instantiated it. Typically you use the key word 'new' to do that.
Was This Post Helpful? 0
  • +
  • -

#5 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 08:16 AM

so how does this look..
Public Class frmMain
    Dim randN As Random
    Dim randNum1 As Integer = randN.Next(1, 5)
    Dim randNum2 As Integer = randN.Next(1, 5)
    Dim randNum3 As Integer = randN.Next(1, 5)
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim randNum1 As New Random
        Dim randNum2 As New Random
        Dim randNum3 as New Random
    End Sub

..?

This post has been edited by modi123_1: 17 October 2013 - 08:21 AM
Reason for edit:: use code tags

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8897
  • View blog
  • Posts: 33,364
  • Joined: 12-June 08

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 08:22 AM

That doesn't make much sense. You are declaring randN in line 2.. and then using that variable (which is not new or instantiated) in line 3, 4, 5. You can't use a variable you haven't given memory to.
Was This Post Helpful? 0
  • +
  • -

#7 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 08:24 AM

View PostYeaH_IKnoW13, on 17 October 2013 - 08:16 AM, said:

so how does this look..
Public Class frmMain
    Dim randN As Random
    Dim randNum1 As Integer = randN.Next(1, 5)
    Dim randNum2 As Integer = randN.Next(1, 5)
    Dim randNum3 As Integer = randN.Next(1, 5)
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim randNum1 As New Random
        Dim randNum2 As New Random
        Dim randNum3 as New Random
    End Sub

..?

Ok Thanks for fixing it!
Was This Post Helpful? 0
  • +
  • -

#8 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 08:33 AM

View Postmodi123_1, on 17 October 2013 - 08:22 AM, said:

That doesn't make much sense. You are declaring randN in line 2.. and then using that variable (which is not new or instantiated) in line 3, 4, 5. You can't use a variable you haven't given memory to.

OK Ok I finally understood what you meant by your last comment, it isn't giving me that error anymore! Now the program starts but the spin button does not work. Any ideas?
Public Class frmMain
    Dim randN As Random
   
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
        
    End Sub

    Private Sub btnSpin_Click(sender As Object, e As EventArgs) Handles btnSpin.Click
        randN = New Random
        Dim randNum1 As Integer = randN.Next(1, 5)
        Dim randNum2 As Integer = randN.Next(1, 5)
        Dim randNum3 As Integer = randN.Next(1, 5)



        If randNum1 = 1 And randNum2 = 1 And randNum3 = 1 Then
            lbl1.Text = Convert.ToString(randNum1)
            lbl2.Text = Convert.ToString(randNum2)
            lbl3.Text = Convert.ToString(randNum3)
            lblWonOrLost.Text = "You Won!"
        ElseIf randNum1 = 2 And randNum2 = 2 And randNum3 = 2 Then
            lbl1.Text = Convert.ToString(randNum1)
            lbl2.Text = Convert.ToString(randNum2)
            lbl3.Text = Convert.ToString(randNum3)
            lblWonOrLost.Text = "You Won!"
        ElseIf randNum1 = 3 And randNum2 = 3 And randNum3 = 3 Then
            lbl1.Text = Convert.ToString(randNum1)
            lbl2.Text = Convert.ToString(randNum2)
            lbl3.Text = Convert.ToString(randNum3)
            lblWonOrLost.Text = "You Won!"
        ElseIf randNum1 = 4 And randNum2 = 4 And randNum3 = 4 Then
            lbl1.Text = Convert.ToString(randNum1)
            lbl2.Text = Convert.ToString(randNum2)
            lbl3.Text = Convert.ToString(randNum3)
            lblWonOrLost.Text = "You Won!"
        Else
            lblWonOrLost.Text = "You Lose."
        End If
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#9 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 177
  • View blog
  • Posts: 1,087
  • Joined: 11-May 12

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 08:35 AM

We need more information that "doesn't work" are there new errors, is it not running at all?
Was This Post Helpful? 0
  • +
  • -

#10 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 08:37 AM

View Postybadragon, on 17 October 2013 - 08:35 AM, said:

We need more information that "doesn't work" are there new errors, is it not running at all?

There is no error, I click start and then proceed to click the spin button and it does nothing at all.
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8897
  • View blog
  • Posts: 33,364
  • Joined: 12-June 08

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 08:44 AM

Okay.. so put a break point in the event function and step through each line to see what is happening. Basic Debugging 101.
Was This Post Helpful? 0
  • +
  • -

#12 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,499
  • Joined: 16-March 11

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 08:52 AM

Place a breakpoint on the first line of the button click code. Press f11 and run through it line by line. Watch the behavior, mouse over your variables in the if statement as you go through it. By mousing over you can see the variables values.
Was This Post Helpful? 0
  • +
  • -

#13 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: Slot Machine, Error displayed.

Posted 17 October 2013 - 10:07 AM

Guys I figured out the problem. It was that after the Else code, I didn't have the output lines. That would display the number. It was displaying the " you lose " and then you win afterwards as random. Thanks for all your guys help !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1