Toribash
Original Post
Help with VB assignment
Guys. for the last hour I worked on this fucker
Public Class Form1
    Dim inttextinput As Integer
    Dim intamounttime As Integer
    Dim intanswer As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtinput.Text = "Please enter a number between 1 and 12"
    End Sub
    Private Sub txtinput_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtinput.Click

        'this notifies the user what number they should enter
        If txtinput.Text = "Please enter a number between 1 and 12" Then
            txtinput.Text = ""
        End If
    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5Timestables.Click

        inttextinput = txtinput.Text + 1
        intamounttime = 0

        For timetable As Integer = 1 To 13
            If inttextinput > 13 Then
                MsgBox("Sorry you can only enter numbers up to 12")
                End
            ElseIf inttextinput < 1 Then
                MsgBox("Sorry you cant enter numbers less than 1")
                End
            End If

            intanswer = intamounttime * txtinput.Text
            MsgBox(intamounttime & "x" & txtinput.Text & "=" & intanswer)
            intamounttime = intamounttime + 1
            If timetable = inttextinput Then

            End If
        Next
        txtinput.Text = "Please enter a number between 1 and 12"
    End Sub


End Class
You're probably wondering what that code does. you probably think it does something awesome. Nope.
Originally Posted by My dickhead bad wording task teacher
Create a times-table program for children to learn their 12 times table.
Children should be able to enter a number between 1 and 12, and then click a button.
When the button is clicked, the program should use a loop to output the ‘times table’ for that particular number.

For example, if the number ‘5’ is entered, the program should display:
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
… all the way up to…
12 x 5 = 60

The output can be in either a label(s), or messagebox(s).
The program should also have an exit button.
If the children enter a number not between 1 and 12, or something that is not a number, the program should display an error message.

. He asked me to do 12 times tables but in the examples 5 is the only reoccurring number so I guess he meant 5 times tables. if not I can change it to 12 by modifying two lines. anyway, If any of you guys know some sort of VB can you confirm to me that this is indeed correct? It's hard to self break a program

ok so I derped a little bit on reading the task. fixed the code
Last edited by RedPanda; Oct 24, 2014 at 01:30 AM.
Life's not a waste of time and time's not a waste of life so let's stop wasting time, get wasted and have the time of our lives - Mr Worldwide 3:18
1. Use Option Explicit On
2. Rename your class to something meaningful
3. Before and after a sub/function there should be a new line
4. Don't put a space after the beginning of the sub
5. In VB by convention subs/function start with capital letters unless referencing a hungarian notation variable, in which case the variable should start with a capital letter after the prefix. Variables are camelcased.
6. Give things better names, for real man "button1"? At least btnSubmit
7. What happens if someone enters "one" into your textbox? I don't see any verification to make sure they are numeric
8. Not sure what "if timetable = inttextinput" is supposed to be doing.
9. Addition is faster than multiplication, why not increase efficiency a bit?
10. You should get in the habit of using On Error
11. Oh yeah And you should parse integer!
12. Watch your scoping, you don't need your variables to be class wide.
13. Personally I'd stay away from doing stuff like you do on your txtInput_Click function, I'd just make a label and write "Please enter a number between 1 and 12" there and have an empty textbox next to it.

VB code:

Option Explicit On

Public Class FiveTimesTables

Private Sub FiveTimesTables_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtInput.Text = "Please enter a number between 1 and 12"
End Sub

Private Sub txtInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtinput.Click
'this notifies the user what number they should enter
If txtInput.Text = "Please enter a number between 1 and 12" Then
txtInput.Text = ""
End If
End Sub

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5Timestables.Click
On Error GoTo ErrorHandler

Dim intNumber As Integer
Dim intAnswer As Integer = 0
Dim valid As Integer = Int32.TryParse(txtInput.Text, intNumber)

If Not valid Then
MsgBox("Sorry, you must enter an integer only")
End
ElseIf intNumber > 12 Then
MsgBox("Sorry, you can only enter numbers up to 12")
End
ElseIf intNumber < 1 Then
MsgBox("Sorry, you cant enter numbers less than 1")
End
End If

For intTimesBy As Integer = 1 To 12
intAnswer += intNumber
MsgBox(intTimesBy & "x" & intNumber & "=" & intAnswer)
Next

txtInput.Text = "Please enter a number between 1 and 12"

Exit Sub

ErrorHandler:
MsgBox("An unknown error has occured")
End Sub

End Class


Basically I think it comes down to you thinking of what you would do, not of what a computer should do. Remember a computer doesn't know obvious things like "'sam' isn't a number", it will happily kill itself if you ask it to do 'sam' x 3. You should also consider what actually needs doing in loops, for example calculating number X multiplier every step of the loop is a lot more taxing than just adding the number. Also remember that in a for loop there is an index variable so you don't need to keep track yourself.

I don't know what kind of level this is, I think highschool since it's vb, but if you want to seriously write code then you should think about what kind of code you want to write. Even though you have a lot of freedom with what you can write, you should be strict with yourself.

The other day I had a vb contract and wrote about 1500 lines, I've had enough of vb for now :P It's not such a bad language though.

EDIT: By the way I didn't test anything I just wrote, so there's likely some typos.
<Faint> the rules have been stated quite clearly 3 times now from high staff