Returning Array and assign to array from calling function

surendra
Public Sub DoSomeThing()
    Dim dict As Object
    Dim arr2(5)
    Set arr2() = aaa()

   For m = LBound(arr2) To UBound(arr2)
        Set dict = aaa()(m)
        Dim key As Variant
       For Each key In dict.Keys
           Debug.Print dict(key)
       Next key
  Next
End Sub

Public Function aaa() As Variant
    Dim arr(5)
   Dim dict_123 As Object
   For k = 1 To 2
       If k = 1 Then
           val1 = 300
           val2 = 500
       ElseIf k = 2 Then
           val1 = 600
           val2 = 1200
      End If
       Set dict_123 = CreateObject("Scripting.Dictionary")
       dict_123.Add "first", val1
       dict_123.Add "Second", val2
       Set arr(k) = dict_123
   Next
  aaa = arr
End Function

Here I want to get return the Array from aaa to the DoSomething and process that array from DoSomeThing. How can I do that?

I am getting the error as can't assign to Array

QHarr

There are a lot of errors and I am not sure of what you are trying to achieve overall. Your code above "fixed" below.

Notes:

  1. arr2 = aaa set one array equal to the other (no set keyword as not object). Do not dimension arr2 first.
  2. Test if current array (arr2) item is a dictionary before attempting the set. You have only added dictionaries at index 1 and 2 in the 0 based array. Less robust would be If m = 1 Or m = 2
  3. Use Option Explicit and declare all your variables
  4. I prefer a Select Case in the function to If statement particularly if you want to add more conditions where you may want the same result for more than one condition.

Code:

Option Explicit

Public Sub DoSomeThing()
    Dim dict As Object, arr2, m As Long, key As Variant
    arr2 = aaa  '<==Set one array equal to the other (no set keyword as not object)

    For m = LBound(arr2) To UBound(arr2)
       If TypeName(arr2(m)) = "Dictionary"  ' <== We can test if current array item is a dictionary before attempting the set. You have only added dictionaries at position 1 and 2 in the array. Less robust would be If m = 1 Or m = 2
            Set dict = arr2(m)   '<==index into your arr2 array
            For Each key In dict.Keys
                Debug.Print dict(key)
            Next key
        End If
    Next
End Sub

Public Function aaa() As Variant
   Dim arr(5), k As Long, val1 As Long, val2 As Long, dict_123 As Object
   For k = 1 To 2
       Select Case k '<== Use select statement 
       Case 1
           val1 = 300
           val2 = 500
       Case 2
           val1 = 600
           val2 = 1200
      End Select
      Set dict_123 = CreateObject("Scripting.Dictionary")
      dict_123.Add "first", val1
      dict_123.Add "Second", val2
      Set arr(k) = dict_123 'K starts at 1 so position 0 is empty; as are positions after 2.
   Next k
   aaa = arr

End Function

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling specific element from array not returning (Ruby)

From Dev

Returning an Array from a Function in C

From Dev

Returning a mutable array from a function

From Dev

Returning an array from a function in VBA

From Dev

Returning an array of pointers from a function

From Dev

Returning an array from function in nodejs

From Dev

Returning vector array from a function

From Dev

Returning an array from function in C

From Dev

Javascript returning array from the function

From Dev

Value won't assign to array after calling function Angular 9

From Java

Returning an array without assign to a variable

From Dev

Issue with returning an array of type int from function

From Dev

Returning an Associative Array from an awk function

From Dev

PHP function not returning a value from multidimensional array

From Dev

Returning array address from function did not work

From Dev

returning a bash array from a bash function

From Dev

returning array from function in c++

From Dev

returning array from function and assigning it to variable

From Dev

Wrong array values returning from a function

From Dev

returning array of string from function not working as expected

From Dev

Returning an array from function to main sub

From Javascript

Returning an array from a JSX function and destructuring it elsewhere

From Dev

Returning an array from a higher order function

From Dev

Type mismatch when returning an array from a function

From Dev

Returning an array from a function with arguments in c++

From Dev

Returning empty array from generic function

From Dev

Python: Returning array values from a function

From Dev

Explain syntax for returning array by reference from a function

From Dev

PHP array value not returning from a function?

Related Related

  1. 1

    Calling specific element from array not returning (Ruby)

  2. 2

    Returning an Array from a Function in C

  3. 3

    Returning a mutable array from a function

  4. 4

    Returning an array from a function in VBA

  5. 5

    Returning an array of pointers from a function

  6. 6

    Returning an array from function in nodejs

  7. 7

    Returning vector array from a function

  8. 8

    Returning an array from function in C

  9. 9

    Javascript returning array from the function

  10. 10

    Value won't assign to array after calling function Angular 9

  11. 11

    Returning an array without assign to a variable

  12. 12

    Issue with returning an array of type int from function

  13. 13

    Returning an Associative Array from an awk function

  14. 14

    PHP function not returning a value from multidimensional array

  15. 15

    Returning array address from function did not work

  16. 16

    returning a bash array from a bash function

  17. 17

    returning array from function in c++

  18. 18

    returning array from function and assigning it to variable

  19. 19

    Wrong array values returning from a function

  20. 20

    returning array of string from function not working as expected

  21. 21

    Returning an array from function to main sub

  22. 22

    Returning an array from a JSX function and destructuring it elsewhere

  23. 23

    Returning an array from a higher order function

  24. 24

    Type mismatch when returning an array from a function

  25. 25

    Returning an array from a function with arguments in c++

  26. 26

    Returning empty array from generic function

  27. 27

    Python: Returning array values from a function

  28. 28

    Explain syntax for returning array by reference from a function

  29. 29

    PHP array value not returning from a function?

HotTag

Archive