Reputation: 183
Just wondering if anyone can help me with this slight issue. I'm writing a simple multiplications table using a list box to display the times tables when a user clicks a button. However, the numerous loops look quite messy and I would like to place them in a method/function that will allow the manipulation of integer values in the button click events so that the correct values can be inserted i.e 2x times would have a valueOne = 2 To valueTwo = 24 and Step = 2 (referencing the 2 times table) etc This is what I currently have just now (based on 7 times table example) but I feel I have not written my sub correctly. Any pointers and advice would be much appreciated and helpful. Thanks.
Note: Variables are declared globally
Private Sub newLoop()
For xTablesAnswer As Integer = valueOne To valueTwo Step valueThree
xTableNumOrder = xTableNumOrder + 1
lstData.Items.Add("7 Times " & xTableNumOrder.ToString & " = " & xTablesAnswer.ToString)
Next
End Sub
Private Sub bnt7X_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnt7X.Click
ClearList()
valueOne = 7
valueTwo = 84
valueThree = 7
newLoop()
End Sub
End Class
Upvotes: 1
Views: 1508
Reputation: 17875
I would do it like this:
Public Sub ShowTable(value As Integer, lastMultiple As Integer)
lstData.Items.Clear()
For i As Integer = 1 to lastMultiple
lstData.Items.Add(String.Format("{0} times {1} = {2}", value, i, value * i))
Next
End Sub
Then for 7 times table, you would call it like this:
ShowTable(7, 12)
Upvotes: 2