Hi guys,

Question 1. I'm a pretty rubbish coder, haven't really done much of it in the couple of years since my degree, at work they've recently expressed an interest in getting me back into it. My problem is that i feel as though I've been taught the syntax (which i totally understand) I can easily write up a a for loop or whatever, my problem is knowing how to apply that. So i guess I would define it as knowing the words of another language but not having any idea how to speak fluently lol. Any ideas where i can start getting some help ? useful links would be appreciated, preferably anything that helped you guys that is more about how to code rather than syntax (i plan to spend more time on here etc).

Question 2, which is sort of an example of my problem (see below), I actually broke this when trying to fix it (trying to add in the return 1,2,3 for the different possibilities), but basically I'm currently struggling with using forms. Using 2 random numbers the user must enter the correct answer for multiplying the random numbers. How can I empty the fields and basically reset the form after a correct answer ?

Public Class MathLoader

    Dim generator As New Random

    Dim a1 As Integer = generator.Next(1, 100)
    Dim b1 As Integer = generator.Next(1, 100)


    Public MathLoader()


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub


    Private Function correctCheck() As Integer

        Dim z1 As Double = a1 * b1
        Dim x1 As Integer = 0

        If TextBox1.Text = "" Then
            Label3.Text = "Please enter a value"
            Return 2

        ElseIf TextBox1.Text = z1 Then     

            Label3.Text = "Correct !"
            ans1.Text = z1
            Beep()
            Return 1

        Else
            Label3.Text = "Wrong..."
            Return 3

        End If

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Try

        ran1.Text = a1
        ran2.Text = b1

        If ans1.Text = "" Then
            MessageBox.Show("Please enter a response")

        ElseIf ans1.Text <> "" Then
            For index = 1 To 3
                If Me.correctCheck() = 1 Then
                    MessageBox.Show("Complete")
                ElseIf Me.correctCheck() = 2 Then
                    MessageBox.Show("2")
                ElseIf Me.correctCheck() = 3 Then
                    MessageBox.Show("3")
                Else
                    MessageBox.Show("4")
                End If
            Next
            MessageBox.Show("You're out of guesses")

        End If

        'Catch e As Exception
        '    MessageBox.Show(e.ToString)

        'End Try

   End Sub

End Class

Recommended Answers

All 2 Replies

Member Avatar for Unhnd_Exception

I learned how to code by writing code. I refer to the msdn library because it is the instructions on how to use the language. Your pretty much just going to have to dive right into it. It will work itself out naturally. When you get stuck find the answer and keep going. Can't find the answer post it here.


Your above code could look like below. Probably not going to do you any good other than seeing some other code thats not yours. You should look at the variable names though. One thing you can start right now is naming your variables with a meaningful name. You will end up doing it eventually if you like to learn the hard way. You could also start now as your beginning.

Public Class Form3

    Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadRandomValues()
    End Sub

    Private Sub ButtonGuessAnswer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGuessAnswer.Click
        'Use static to keep track of how many tries.
        Static NumberOfGuesses As Integer

        'Should be self explanatory
        If String.IsNullOrEmpty(TextBoxAnswer.Text) Then
            MessageBox.Show("Please enter a response.")
        ElseIf Not IsNumeric(TextBoxAnswer.Text) Then
            MessageBox.Show("Wrong.")
        Else
            Dim Number1 As Double = CDbl(LabelRandomNumber1.Text)
            Dim Number2 As Double = CDbl(LabelRandomNumber2.Text)

            If CDbl(TextBoxAnswer.Text) = Number1 * Number2 Then
                MessageBox.Show("Correct.")
                'Got it right. Start over.
                LoadRandomValues()
                NumberOfGuesses = 0
            Else
                MessageBox.Show("Wrong.")
            End If
        End If

        NumberOfGuesses += 1

        If NumberOfGuesses = 3 Then
            'Tried three times and couldn't get it.
            'reset the number of guesses and reset
            'the display
            MessageBox.Show("Your Out of guesses.")
            LoadRandomValues()
            NumberOfGuesses = 0
        End If
    End Sub

    Private Sub LoadRandomValues()
        'Resets the screen with new values
        Dim Generator As New Random

        LabelRandomNumber1.Text = Generator.Next(1, 100)
        LabelRandomNumber2.Text = Generator.Next(1, 100)
        TextBoxAnswer.Clear()
    End Sub

End Class

Thank you so much, that is so useful, just seeing how to write code correctly is beneficial and agree it is best to just jump in the deep end. I will be posting again very soon. Thank you !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.