none
I'm getting an error code while my friend is not. RRS feed

  • Question

  • I'm coding for a project in a programming class. We are supposed to create a pair of dice 1000 times and find all the times they add up to 7. My friend and I have the same exact code and his works and mine will not. We even have the same error code yet mine is not letting me even start the project. It keeps saying that I don't have the project.exe in the debug folder while I checked and I did. Here's the code that we have. The error is occurring on the button click and I don't understand why.
    Public Class Form1
    
        Public Sub Button1_Click(sender As Object, e As EventArgs, ByVal pair1 As Integer, ByVal pair2 As Integer) Handles Button1.Click
            Label1.Text = pair1.ToString
            Label2.Text = pair2.ToString
            If pair1 > pair2 Then
                MessageBox.Show("Player 1 wins!")
            ElseIf pair2 > pair1 Then
                MessageBox.Show("Player 2 wins!")
            ElseIf pair1 = pair2 Then
                MessageBox.Show("Player 1 and Player 2 tied!")
            End If
        End Sub
    
    
    End Class
    
    Public Class HighRoller
        Dim rnd As Random
    
        Dim pair1 As Integer
        Dim pair2 As Integer
        Dim dice1 As New PairOfDice1(rnd)
        Dim dice2 As New PairOfDice1(rnd)
    
        Dim dice3 As New PairOfDice2(rnd)
        Dim dice4 As New PairOfDice2(rnd)
    
        Dim roll1 As Integer = dice1.roll
        Dim roll2 As Integer = dice2.roll
        Dim roll3 As Integer = dice3.roll
        Dim roll4 As Integer = dice4.roll
        Public Sub New(ByVal roll1 As Integer, roll2 As Integer, roll3 As Integer, roll4 As Integer)
            pair1 = roll1 + roll2
            pair2 = roll3 + roll4
    
        End Sub
    
    
    End Class
    
    Public Class PairOfDice1
        Dim num1 As Integer
        Private rnd1 As Random
    
        Public Sub New(ByVal random As Random)
            rnd1 = random
    
        End Sub
    
        Public Function roll() As Integer
            Randomize()
            num1 = Int(Rnd() * 6) + 1
            Return num1
    
        End Function
    End Class
    
    Public Class PairOfDice2
        Dim num2 As Integer
        Private rnd1 As Random
    
        Public Sub New(ByVal random As Random)
            rnd1 = random
    
        End Sub
    
        Public Function roll() As Integer
            Randomize()
            num2 = Int(Rnd() * 6) + 1
            Return num2
    
        End Function
    End Class

    Tuesday, May 1, 2018 5:49 PM

All replies

  • Hi

    Try putting Option Strict On at the top of your code and recompile - then fix all the errors.


    Regards Les, Livingston, Scotland

    Tuesday, May 1, 2018 5:52 PM
  • Please post the exact message and the exact line getting the error.

    When you get the error while debugging, the window you see probably has a link for copying the exception information to the clipboard. You can paste that here or paste temporarily elsewhere and then copy and paste the parts you want to here.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, May 1, 2018 6:00 PM
  • Ok so I did that but now when I click the button, nothing happens. It opens the form now but nothing else besides that.
    Tuesday, May 1, 2018 6:02 PM
  • Error 1 Method 'Public Sub Button1_Click(sender As Object, e As System.EventArgs, pair1 As Integer, pair2 As Integer)' cannot handle event 'Public Event Click(sender As Object, e As System.EventArgs)' because they do not have a compatible signature. C:\Users\steverdevin07\Documents\Visual Studio 2012\Projects\WindowsApplication56\WindowsApplication56\Form1.vb 3 128 WindowsApplication56

    this is what it says

    Tuesday, May 1, 2018 6:03 PM
  • Hi

    You have added parameters to the default Button click event signature. That won't work. You could have those Pair1 and Pair 2 Integers declared as Form wide variables and access them anywhere in your code.


    Regards Les, Livingston, Scotland

    Tuesday, May 1, 2018 6:21 PM
  • I think there is few change you get a project running with that name 

    "Form1.vb 3 128 WindowsApplication56"

    I don't know how you managed to do that, but take first in your 

    Project -> Project Properties and look what is strange there. 


    Success
    Cor


    Tuesday, May 1, 2018 6:30 PM
  • So how would be the best way to go about this. I tried just dimming the variables again in the Class Form1 but then it just resets the variables to 0.
    Public Class Form1
        Dim pair1 As Integer
        Dim pair2 As Integer
    
        Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Label1.Text = pair1.ToString
            Label2.Text = pair2.ToString
            If pair1 > pair2 Then
                MessageBox.Show("Player 1 wins!")
            ElseIf pair2 > pair1 Then
                MessageBox.Show("Player 2 wins!")
            ElseIf pair1 = pair2 Then
                MessageBox.Show("Player 1 and Player 2 tied!")
            End If
        End Sub
    
    
    End Class
    
    Public Class HighRoller
        Dim rnd As Random
    
        Dim pair1 As Integer
        Dim pair2 As Integer
        Dim dice1 As New PairOfDice1(rnd)
        Dim dice2 As New PairOfDice1(rnd)
    
        Dim dice3 As New PairOfDice2(rnd)
        Dim dice4 As New PairOfDice2(rnd)
    
        Dim roll1 As Integer = dice1.roll
        Dim roll2 As Integer = dice2.roll
        Dim roll3 As Integer = dice3.roll
        Dim roll4 As Integer = dice4.roll
        Public Sub New(ByVal roll1 As Integer, roll2 As Integer, roll3 As Integer, roll4 As Integer)
            pair1 = roll1 + roll2
            pair2 = roll3 + roll4
    
        End Sub
    
    
    End Class
    
    Public Class PairOfDice1
        Dim num1 As Integer
        Private rnd1 As Random
    
        Public Sub New(ByVal random As Random)
            rnd1 = random
    
        End Sub
    
        Public Function roll() As Integer
            Randomize()
            num1 = Int(Rnd() * 6) + 1
            Return num1
    
        End Function
    End Class
    
    Public Class PairOfDice2
        Dim num2 As Integer
        Private rnd1 As Random
    
        Public Sub New(ByVal random As Random)
            rnd1 = random
    
        End Sub
    
        Public Function roll() As Integer
            Randomize()
            num2 = Int(Rnd() * 6) + 1
            Return num2
    
        End Function
    End Class

    Tuesday, May 1, 2018 6:37 PM
  • You calculate pair1 and pair2 in the HighRoller's constructor, right? You don't show where you create a HighRoller object but you need to use it to get pair1 and pair2. The HighRoller object should be a member of the Form1 class and then you can use it in the button click.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, May 1, 2018 6:54 PM
  • I'm pretty new to coding so I'm not 100% sure what you are saying, but I think I understand. The problem is the project is requiring it to be calculated in the HighRoller class, so I need to bring the variable from the other class.
    Tuesday, May 1, 2018 7:11 PM
  • You need to create (dim) a HighRoller in Form1 then use Pair1 and pair2 in that in the button click. Your HighRoller constructor takes roll1 thru roll4 and I don't know where they are supposed to come from; I assume the form so that is the complicated part.

    This paragraph might be a bit too advanced for you and it probably can be ignored. Perhaps you need to create a HighRoller using the default constructor (in other words without roll1 thru roll4) and fill it in later but then the calculation won't be done. I assume you don't know what properties are (how they work) but that would be a good way to solve the problem. For now you can just create a new HighRoller when you need to set the roll1 thru roll4.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, May 1, 2018 8:38 PM
  • The answer to the specific error was given, however, there is a lot of unnecessary complication in this code.

    Let's back up for a minute and think about the requirement.  The primary object you need to model is a pair of dice.  This does require a single instance of Random, and a Roll() method.  The instance of Random can be initialized within the class definition.  The roll method then only needs to get two values between 1 and 6 and return their sum.

    Consider something like the following:

    Public Class PairOfDice
        Private randomNumber As New Random
    
        Public Function Roll() As Integer
            Dim roll1 As Integer = 'what do you think goes here?
            Dim roll2 As Integer = 'what do you think goes here?
            Return roll1 + roll2
        End Function
    End Class

    Reference the Random.Next method to determine what values you should use.

    Now you need to make 1000 rolls and count the number of sevens.  For that, you just need a loop something like this:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim dice As New PairOfDice
        Dim count As Integer
        For 'what goes here?
            Dim roll As Integer = 'how will you get the value of a roll?
            If roll = ? Then 'what should you do?
        Next
        Label1.Text = $"The number 7 was rolled {count} times."
    End Sub
    
    I've tried to provide you with the general idea while still leaving some particulars for you to figure out on your own.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 1, 2018 9:32 PM
    Moderator
  • Some simple classes.  Note that only one Random object is created.

    Public Class Die
        Private Shared PRNG As New Random
        Private Sides As Integer = -1
        Private LastThrow As Integer = -1
    
        Public Sub New(Optional NumOfSides As Integer = 6)
            Me.Sides = NumOfSides
        End Sub
    
        Public Function ThrowDie() As Integer
            Me.LastThrow = PRNG.Next(1, Me.Sides + 1)
            Return Me.LastThrow
        End Function
    End Class
    
    Public Class DiceGame
        Private Dice As New List(Of Die)
        Public NumOfThrows As Integer = 0
        Public Stats As New SortedDictionary(Of Integer, Integer)
    
        Public Sub New(Optional NumOfDie As Integer = 2)
            For x As Integer = 1 To NumOfDie
                Me.Dice.Add(New Die)
            Next
        End Sub
    
        Public Function ThrowThem() As Integer
            Me.NumOfThrows += 1
            Dim tot As Integer = 0
            For Each d As Die In Me.Dice
                tot += d.ThrowDie()
            Next
            If Not Me.Stats.ContainsKey(tot) Then
                Me.Stats.Add(tot, 0)
            End If
            Me.Stats(tot) += 1
            Return tot
        End Function
    
        Public Function PerCent7() As Double
            Return Me.Stats(7) / Me.NumOfThrows
        End Function
    
    End Class
    

    To use

        Dim foo As New DiceGame
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For x As Integer = 1 To 10000
                foo.ThrowThem()
            Next
            Debug.WriteLine(foo.PerCent7.ToString("p1"))
            Debug.WriteLine("")
            For Each kvp As KeyValuePair(Of Integer, Integer) In foo.Stats
                Debug.WriteLine("{0,-5}{1,-10}{2:p1}", kvp.Key, kvp.Value, kvp.Value / foo.NumOfThrows)
            Next
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Tuesday, May 1, 2018 9:39 PM

  • La vida loca

    Tuesday, May 1, 2018 11:03 PM