# Array appears to be empty after passing to a function?

**URL:** <https://forums.powershell.org/t/array-appears-to-be-empty-after-passing-to-a-function/4303>\
**Category:** PowerShell Help\
**Created:** [June 2, 2015, 7:33pm UTC](https://forums.powershell.org/t/array-appears-to-be-empty-after-passing-to-a-function/4303 "2015-06-02T19:33:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dean-warren](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dean-warren](https://forums.powershell.org/u/dean-warren)\
**Post date:** [June 2, 2015, 7:33pm UTC](https://forums.powershell.org/t/array-appears-to-be-empty-after-passing-to-a-function/4303/1 "2015-06-02T19:33:22Z")

</div>

Array appears to be empty after passing to a function? That is the array has the correct number of elements, but not the content.

e.g.

```
function createArray ( [int]$length )
{
    $array = @("") * $length
    return $array
}
```

```
function doSomethingWithArray( [string[]]$myArray )
{
    $i = 0
    foreach ($e in $myArray)
    {
        write-host $i.tostring()
        write-host $e.tostring()
        $i++
    }
}
```

```
$myArray = createArray 3
doSomethingWithArray $myArray
```

Output is somehting like  
0

1

2

Note the gaps? Note I have to do write-host $e.tostring() as the element is not a string?

Is the array wrapped as an object? are the elements wrapped as an object? Is this a scope issue?

Please help ASAP.

Thanks

---

<div class="post-metadata">

**Author:** ![\_timpringle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/_timpringle/32/276_2.png) [@\_timpringle](https://forums.powershell.org/u/_timpringle)\
**Post date:** [June 2, 2015, 8:33pm UTC](https://forums.powershell.org/t/array-appears-to-be-empty-after-passing-to-a-function/4303/2 "2015-06-02T20:33:39Z")

</div>

Hi Dean

ASAP is what you pay contractors for… 😉

I think you’re possibly getting confused between creating the array and actually populating it. The gaps are occurring because you’re trying to write to the screen empty elements in the array. You only define a static array of three elements, you are not populating it.

You’re seeing the space because it’s writing out the blank value of the array with a line feed at the end. This is standard behaviour.

Not certain what you mean about the .tostring, my code below works in the same way as yours without the .tostring.

```
function createArray ( [int]$length )
{
    $array = @("") * $length
    return $array
}
function doSomethingWithArray( [string[]]$myArray ) {
    $i = 0
    foreach ($e in $myArray)
    {
        write-host $i
        write-host $e
        $i++
    }
}
$myArray = createArray 3
doSomethingWithArray $myArray
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [June 3, 2015, 1:05am UTC](https://forums.powershell.org/t/array-appears-to-be-empty-after-passing-to-a-function/4303/3 "2015-06-03T01:05:36Z")

</div>

A side note, you don’t need to pre-create arrays and then fill them such as in VBScript DIM and REDIM. Arrays are really simple and Powershell will convert any of this to an array:

```
#Powershell will automatically create this as an array
$array1 = "Computer1", "Computer2", "Computer3"
#Casting option to force into and array
[array]$array2 = 1, 2, 3
#Another casting option using a @()
$array3 = @("Dog", "Cat", "Fish")

#Add a value to an array
$array1 += "Computer4"

#Array using a range
$array4 = 1..5

#An array of arrays
$array1, $array2, $array3, $array4 | foreach{
    $_.GetType()
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/array-appears-to-be-empty-after-passing-to-a-function/4303/4 "2024-05-16T20:45:23Z")

</div>

Closed on May 16, 2024, 8:45pm UTC


