1

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
2
  • Meta-Knights solution below is very appropriate. In your code above, you can just add in the valueOne, valueTwo, and valueThree as parameters in your newLoop function. (i.e. newLoop(ByVal valueOne as integer, ByVal valueTwo as integer, ByVal valueThree as integer), then just call it in your Button click (i.e. newLoop(7,84,7). But as I said Meta-Knights solution is more elegant. Commented Feb 1, 2012 at 3:35
  • Yeah I really liked Meta-Knights solution, it feels more "efficient" and tidy so to speak in comparison to my messy code. I just neeed to understand what is happening with the formatting though "("{0} times {1} = {2}", value, i, value * i))" then I can sleep happily lol well, at least until the next problem arises. Commented Feb 1, 2012 at 6:14

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

That is perfect, working like a charm, thank you very much. Without meaning to be rude though, I am fairly new to programming but can you explain this line? ("{0} times {1} = {2}", value, i, value * i)) as I am only familiar with only a basic formatting such as this : "String.Format("{0:n3}", dblNumber)" etc Thanks for your help though!
@wilbomc - The {0}, {1}... values represent the index of the argument you want to display. The second parameter of the String.Format method is a list of parameters, so in your scenario {0} will display the value, {1} will display i and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.