1

I have the following code:

[Collections.Generic.List[String]]$script:Database = New-Object Collections.Generic.List[String]
[string]$script:DatabaseFile = "C:\NewFolder\database.csv"

function RunProgram
{
    $script:Database = getCurrentDatabase #errors after this line
}
function getCurrentDatabase
{
    [string[]]$database = Get-Content -Path $script:DatabaseFile
    [Collections.Generic.List[String]]$databaseAsList = $database
    return $databaseAsList
}

I get this exception after getCurrentDatabase returns:

Cannot convert the "system.object[]" value of type "system.object[]" to type "system.collections.generic.list`1[system.string]"

To get the code to work, I need to do this:

[Collections.Generic.List[String]]$script:Database = New-Object Collections.Generic.List[String]
[string]$script:DatabaseFile = "C:\NewFolder\database.csv"

function RunProgram
{
    getCurrentDatabase #this works fine
}
function getCurrentDatabase
{
    [string[]]$database = Get-Content -Path $script:DatabaseFile
    $script:Database = $database
}

Why does the first way throw that exception, but the 2nd way doesn't?

EDIT: I am using PS version 2.0 and C:\NewFolder\database.csv contains this one line:

Release Group,Email Address,Template ID,Date Viewed
7
  • Can you post the content of your file? Commented May 26, 2014 at 1:55
  • Hi chappoo, see my edit. Commented May 26, 2014 at 2:00
  • I tried running your first snippet and it runs without errors (including invoking RunProgram and writing $script:Database to host)... is that the exact code in your script? Commented May 26, 2014 at 2:05
  • note, there's a syntax error on line 11 (missing a bracket): [Collections.Generic.List[String]]$databaseAsList = $database Commented May 26, 2014 at 2:06
  • sorry that typo wasn't in my original code. and yes this is the entire code. Could it be something to do with the fact that I'm using Powershell v2.0? Commented May 26, 2014 at 2:17

1 Answer 1

2

That's something people trip on a lot... When you output collections from function PowerShell will enumerate them, loosing original type as a part of the process. To prevent it from happening you can use unary comma:

function getCurrentDatabase
{
    [string[]]$database = Get-Content -Path $script:DatabaseFile
    [Collections.Generic.List[String]]$databaseAsList = $database
    , $databaseAsList
}

Unary comma should prevent PowerShell from enumerating your collection (it actually enumerates collection of one that we've just created using unary comma).

I wrote a blog post about it a while ago - should help understand it in more detail.

Sign up to request clarification or add additional context in comments.

3 Comments

wow, I'm sure you can understand how unintuitive that would be for programmers who started with C#!
do you know why my code would have worked for Chappoo? He wrote in the comments below my question that it worked for him.
I would guess it was PowerShell version: when I tried on v3 it worked just fine, had to test it with powershell -version 2 to get same errors you got.

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.