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
RunProgramand writing$script:Databaseto host)... is that the exact code in your script?[Collections.Generic.List[String]]$databaseAsList = $database