Question

Hello I am having a hard time with arrays in visual basic. This is a simple console application (I am trying to get the hang of the syntax before I move on to gui) and all this program does is use two types of arrays jagged and the regular type. This console application is a times table generator i.e enter in 5 columns and 5 rows to make a 5X5 times table. The program is not finished yet and I know the times table wont generate right with this code so far but what I need help with is how to populate arrays in VB. Where my problem is in this sub

SUB:

Sub arrayPopulate(ByVal regularArray(,) As Integer, ByVal columns As Integer, ByVal rows As Integer)
        Dim i As Integer
        Dim j As Integer
        Dim mult As Integer

        For i = 0 To rows
            For j = 0 To columns
                mult = (i + 1) * (j + 1)
                regularArray(j, i) = mult
            Next
        Next

    End Sub

Specifically the line regularArray(j, i) = mult I would have thought it be simple the arrays element is set = to what ever the mult is and the for loops would cover the 2d array. What am I doing wrong and how could I fix it or do it better?

FULL CODE:

 Module Module1
    Sub Main()
        'Declarations
        Dim awns As Char
        Dim switchOption As Integer
        Dim columns As Integer
        Dim rows As Integer
        Dim regularArray(columns, rows) As Integer 

        'Starting Prompts
        Console.WriteLine("Hello this program will create a times table with")
        Console.WriteLine("user inputs in terms of rows and columns.")
        Console.WriteLine("Pick between these two options.")
        Console.WriteLine("Option 1: Times table with a regular array.")
        Console.WriteLine("Option 2: Times table with a jagged array.")

        Do
            Console.Write("Which option do you wnat? ")
            switchOption = Console.ReadLine

            Console.WriteLine("How many columns do you wnat? ")
            columns = Console.ReadLine
            columns = columns - 1
            Console.WriteLine("How many rows do you wnat? ")
            rows = Console.ReadLine
            rows = rows - 1
            Select Case switchOption
                Case 1
                    arrayPopulate(regularArray, columns, rows)
                    Dim i As Integer
                    Dim j As Integer

                    For j = 0 To rows
                        For i = 0 To columns
                            Console.WriteLine("{0}: ", regularArray(i, j))
                        Next
                    Next

                Case 2
                    Console.WriteLine("Test")

            End Select

            Console.WriteLine("Do you want to run again y/n?")
            awns = Console.ReadLine()
        Loop Until awns = "n"
    End Sub

    Sub arrayPopulate(ByVal regularArray(,) As Integer, ByVal columns As Integer, ByVal rows As Integer)
        Dim i As Integer
        Dim j As Integer
        Dim mult As Integer

        For i = 0 To rows
            For j = 0 To columns
                mult = (i + 1) * (j + 1)
                regularArray(j, i) = mult
            Next
        Next

    End Sub
End Module
Was it helpful?

Solution

Where you declared Dim regularArray(columns, rows) As Integer, it makes use of the values of columns and rows as they were at the time; in this case, they were both zero, and hence regularArray is a two-dimensional array having one element - namely, regularArray(0, 0). Use ReDim to change the size of its dimensions. E.g., put this just before the Select Case switchOption:

ReDim regularArray(columns, rows)

See http://msdn.microsoft.com/en-us/library/w8k3cys2.aspx for details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top