0

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
3
  • The function doesn't return any values - it does. You are just throwing them away. Commented Sep 5, 2020 at 18:25
  • 1
    Delete the local variable named FutureArray to get ahead. Commented Sep 5, 2020 at 18:33
  • You need to change Console.WriteLine(FutureArray(ArrayRows, ArrayColumns)) to use the i and j variables, see answers below. Commented Sep 8, 2020 at 13:12

2 Answers 2

2

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

Comments

0

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

2 Comments

OK, Have made the recommend changes but getting errors BC30672 and BC31087 on the line - Dim FutureArray(2, 1) As Object(,) = PopulateFutureArray()
@Michael512, try it without the As Object(,) ? What is the error description for those error numbers that you are getting?

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.