Michael512
Michael512

Reputation: 1

Function not returning Array Values VB

I'm trying to populate an Array using a Function and passing the populated Array back to Main() for more processing. The function doesn't return any values. The Array FutureArray cells are blank. Using VB

MADE THE RECOMMENDED CHANGES. Now I'm Getting errors BC30672 Explicit initialization is not permitted for arrays declared with explicit bounds BC3108 Array modifiers cannot be specified on both a variable and its type

UPDATE; I've resolved the two errors and posted the new code. Still not getting any values returned for FutureArray

Module Module1

    Sub Main()

        Dim FutureArray(,) As Object = PopulateFutureArray()
        Dim ArrayRows As Integer
        Dim ArrayColumns As Integer


        ArrayRows = 2
        ArrayColumns = 1

        For i As Integer = 0 To ArrayRows
            For j As Integer = 0 To ArrayColumns

                Console.WriteLine(FutureArray(ArrayRows, ArrayColumns))

            Next
        Next


    End Sub
    Function PopulateFutureArray() As Object(,)
        'Start of Create Future Dataes Array

        Dim FutureArray(2, 1)

        FutureArray(0, 0) = 10
        Console.WriteLine(FutureArray(0, 0))
        FutureArray(0, 1) = 0
        Console.WriteLine(FutureArray(0, 1))
        FutureArray(1, 0) = 11
        Console.WriteLine(FutureArray(1, 0))
        FutureArray(1, 1) = #11/1/2020#
        Console.WriteLine(FutureArray(1, 1))
        FutureArray(2, 0) = 1
        Console.WriteLine(FutureArray(2, 0))
        FutureArray(2, 1) = 0
        Console.WriteLine(FutureArray(2, 1))


        Return FutureArray

        'End PopulateFutureArray
    End Function


End Module

Upvotes: 0

Views: 130

Answers (2)

Mary
Mary

Reputation: 15081

You have several problems with your code.
A Function must have a datatype. In this case, since you are returning a 2 dimensional array containing more than one datatype, it must be an Object array.
With Option Strict and Option Infer on...

Sub Main()
    Dim ReturnedArray = PopulateFutureArray()
    Dim ArrayRows = 2
    Dim ArrayColumns = 1

    For i As Integer = 0 To ArrayRows
        For j As Integer = 0 To ArrayColumns
            Console.WriteLine(ReturnedArray(i, j).ToString)
        Next
    Next
    Console.ReadLine()
End Sub
Function PopulateFutureArray() As Object(,)

    Dim FutureArray(2, 1) As Object

    FutureArray(0, 0) = 10
    FutureArray(0, 1) = 0
    FutureArray(1, 0) = 11
    FutureArray(1, 1) = #11/1/2020#
    FutureArray(2, 0) = 1
    FutureArray(2, 1) = 0

    Return FutureArray

End Function

Upvotes: 2

Pete -S-
Pete -S-

Reputation: 562

One issue you have is your Console.Writeline is looping through the same indexes.

Console.WriteLine(FutureArray(ArrayRows, ArrayColumns))

should be

Console.WriteLine(FutureArray(i, j))

Here is the updated, working solution:

Module Module1

    Public Sub Main()

        Dim FutureArray As Object(,) = PopulateFutureArray()
        Dim ArrayRows As Integer
        Dim ArrayColumns As Integer

        ArrayRows = 2
        ArrayColumns = 1

        For i As Integer = 0 To ArrayRows
            For j As Integer = 0 To ArrayColumns
                Console.WriteLine(FutureArray(i, j))
            Next
        Next   
    End Sub

    Private Function PopulateFutureArray() As Object(,)
        Dim FutureArray As Object(,)
        ReDim FutureArray(2, 1)

        FutureArray(0, 0) = 10
        FutureArray(0, 1) = 0
        FutureArray(1, 0) = 11
        FutureArray(1, 1) = #11/1/2020#
        FutureArray(2, 0) = 1
        FutureArray(2, 1) = 0

        Return FutureArray
    End Function

End Module

Version 1 of the solution: As @GSerg pointed out, you are calling your function; however, not assigning the results to anything.

Dim FutureArray(2, 1) 
FutureArray = PopulateFutureArray()

Addresses Error BC30672

You have a variable scope issue, even though your variables are named the same they do not share the same memory/storage space.

Function PopulateFutureArray()
        'Start of Create Future Dataes Array
        Dim FutureArray(2, 1)

One thing I'd also point out is to assign the type for the array (Note: this will error, you can't declare and size the array like this, see the complete solution above):

*Dim FutureArray(2, 1) as Object(,)* 
FutureArray = PopulateFutureArray() 

Then change your function to return:

Function PopulateFutureArray() as Object(,)

I'm using object because you have both integer and a date that you are storing. You can read more learn.microsoft.com

Upvotes: 0

Related Questions