r/visualbasic • VB.Net Beginner • Dec 04 '16

[vb2015] Arrays

Good day everyone,

I am working a multidimensional array but for some reason I cannot get the array to display all 20 (4x5) of its items into a textbox.

So far I have this...which displays the first line respectively.

    Dim Rate(,) As Double = {{1, 1.5, 1.65, 1.85}, {1.58, 2, 2.4, 3.05}, {1.71, 2.52, 3.1, 4},_
    2.04, 3.12, 4, 5.01}, {2.52, 3.75, 5.1, 7.25}}


    Dim weightindex, zoneindex, output As Double

    weightindex = ListBox1.SelectedIndex & " "
    zoneindex = ListBox2.SelectedIndex & " "

    output = Rate(weightindex, zoneindex)

    If output < 3 Then
        NRLabel.Text = "$" & (output + output * 0.5).ToString("n2")

    ElseIf output >= 3 Then
        NRLabel.Text = "$" & (output - output * 0.2).ToString("n2")
    End If

    For counter = 0 To 3

        TextBox2.Text &= "$" & Rate(counter, counter).ToString("n2") & " " & vbnewline
    Next

I know my loop should run from 0 to 19 but for some reason, this does not work out...I feel like I should make multiple loops but I believe that would defeat the purpose of using a loop to consolidate. Am I missing a nested loop?

The output should look like the pic on the right below (there is also sorting involved...but I'm trying to grasp that concept as well)...thank you for your time.

http://imgur.com/a/va7nT

6 Upvotes

2 comments sorted by

View all comments

4

u/CtrlShiftAppoint Dec 05 '16 edited Dec 05 '16

You're gonna need 2 loops, this really shouldn't cost that much since the values are so small:

For i As Integer = 0 To 4
  For j As Integer = 0 To 2
    TextBox2.Text &= "$" & Rate(i, j).ToString("n2") & " "
  Next
  TextBox2.Text &= Rate(i, 3) & vbnewline
Next

2

u/Engagethedawn VB.Net Beginner Dec 05 '16 edited Dec 05 '16

Hmm. Ok I will try this out!

Edit:

Wow that worked Great!! Thank you very much. I think I am getting confused with how the variables are interpreted early on vs. variables in the loop and am slowly grasping that you just need an a...b..c...etc. to use in the loop. Thank you for the help.

   For i As Integer = 0 To 4
        For j As Integer = 0 To 2
            TextBox2.Text &= "$" & Rate(i, j).ToString("n2") & " "
        Next
        TextBox2.Text &= "$" & Rate(i, 3).ToString("n2") & vbNewLine
    Next

I even tweaked your code that you gave me so that some of the symbols matched up and the decimals were corrected. At least I can do that... Thanks again!