Unexpected object value returned from Function

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
This topic is 13 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
HONDAAL
Posts: 15
Last visit: Wed Dec 22, 2021 10:02 am
Has voted: 1 time

Unexpected object value returned from Function

Post by HONDAAL »

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.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Unexpected object value returned from Function

Post by davidc »

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
David
SAPIEN Technologies, Inc.
User avatar
HONDAAL
Posts: 15
Last visit: Wed Dec 22, 2021 10:02 am
Has voted: 1 time

Unexpected object value returned from Function

Post by HONDAAL »

That fixed it. Thank you very much for your help and time!
This topic is 13 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.