Returning objects via native PowerShell, including scripts writting using PrimalScript 2009 work as expected. However, the same code in PrimalForms 2009 returns an array. Below is the code and the results. Any help is greatly appreciated. I am posting this here, since the same code works in native PowerShell.
+++++++++++++++++++++++++++++++++++++++++
Native PowerShell code:
# ==============================================================================================# # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009# # NAME: # # AUTHOR: Honda , Honda# DATE : 7/8/2010# # COMMENT: # # ==============================================================================================
Function TestFunction1 { $objDate = Get-Date $message = "Value of object from within TestFunction function: " + $objDate write-host $message
Return $objDate}
Function TestFunction2 { $strValue = "Test" $message = "Value of string from within TestFunction function: " + $strValue write-host $message
Return $strValue}
#Main routine
$strUserID = "mystring" $objTest = TestFunction1 $message = "Value of object returned from TestFunction function: " + $objTest write-host $message $strTest = TestFunction2 $message = "Value of object returned from TestFunction function: " + $strTest Write-Host $message write-host $strTest[0] write-host $strTest[1]
+++++++++++++++++++++++++++++++++++++++++
Results of PowerShell code:
Value of object from within TestFunction function: 07/08/2010 11:22:41Value of object returned from TestFunction function: 07/08/2010 11:22:41Value of string from within TestFunction function: TestValue of object returned from TestFunction function: TestTe*** PowerShell Script finished. ***
+++++++++++++++++++++++++++++++++++++++++
PrimalForms code:
function OnApplicationLoad {
#Note: This function runs before the form is created
#Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path
#Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList)
#Important: Form controls cannot be accessed in this function
#TODO: Add snapins and custom code to validate the application load
return $true #return true for success or false for failure
}
function OnApplicationExit {
#Note: This function runs after the form is closed
#TODO: Add custom code to clean up and unload snapins when the application exits
$script:ExitCode = 0 #Set the exit code for the Packager
}
Function TestFunction1 {
$objDate = Get-Date
$message = "Value of object from within TestFunction function: " + $objDate
$lb1.Items.Add($message)
Return $objDate
}
Function TestFunction2 {
$strValue = "Test"
$message = "Value of string from within TestFunction function: " + $strValue
$lb1.Items.Add($message)
Return $strValue
}
$FormEvent_Load={
#TODO: Initialize Form Controls here
$cb1.Select()
}
$handler_button1_Click={
#TODO: Place custom script here
$lb1.Items.Add("Initializing...")
$lb1.Refresh()
$strUserID = $cb1.Text
$objTest = TestFunction1
$message = "Value of object returned from TestFunction function: " + $objTest
$lb1.Items.Add($message)
$strTest = TestFunction2
$message = "Value of object returned from TestFunction function: " + $strTest
$lb1.Items.Add($message)
$lb1.Items.Add($strTest[0])
$lb1.Items.Add($strTest[1])
}
+++++++++++++++++++++++++++++++++++++++++
Results of PrimalForms code:
Initializing...
Value of object from within TestFunction function: 07/08/2010 11:33:00
Value of object returned from TestFunction function: 1 07/08/2010 11:33:00
Value of string from within TestFunction function: Test
Value of object returned from TestFunction function: 3 Test
3
Test
+++++++++++++++++++++++++++++++++++++++++
It appears that PrimalForms is returning an array instead of just the original object, but I have no idea why it's working differently than native PowerShell. Again, any help is appreciated. Thanks.
Unexpected object value returned from Function
Unexpected object value returned from Function
The $lb1.Items.Add method returns an integer value and that is why you getting an array with the index of the item. This seems to be a PowerShell quirk that I don't quite understand. If you return a specific object it should only return that object, but for some reason PowerShell returns an array if any other object is left in the pipe.
In order to work around this quirk, you have to cast the method to void, which prevents the method from returning a value:
[void]$lb1.Items.Add($message)
You can get the same results by piping the method to Out-Null, but casting to void provides better performance.
Daviddavidc2010-07-08 11:26:51
In order to work around this quirk, you have to cast the method to void, which prevents the method from returning a value:
[void]$lb1.Items.Add($message)
You can get the same results by piping the method to Out-Null, but casting to void provides better performance.
Daviddavidc2010-07-08 11:26:51
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Unexpected object value returned from Function
That fixed it. Thank you very much for your help and time!