Reputation: 19
I'm tryng to return $true or $false from a function, and I get an array . If I remove the listBox messages it works as expected. Does anyone know why?
function TestEmptyFields()
{
$empty= $false
$listBox1.Items.Add("Testing fields")
if ($txtPrtName.get_text()-eq "")
{
$listBox1.Items.Add("Empty name")
$empty= $true
}
elseif ($txtPrtIP.get_text() -eq "")
{
$listBox1.Items.Add("Empty Ip")
$empty= $true
}
else
{
$empty= $false
}
$listBox1.Items.Add($txtPrtName.get_text())
$listBox1.Items.Add($txtPrtIP.get_text())
return $empty
}
But it works fine like this:
function TestEmptyFields()
{
if($txtPrtName.get_text()-eq "")
{
return $true
}
elseif ($txtPrtIP.get_text() -eq "")
{
return $true
}
else
{
return $false
}
}
Upvotes: 2
Views: 1355
Reputation: 26031
In powershell return $empty
is functionally equivalent to $empty ; return
-- that behavior was implemented to make things easier for people with a background in C-style languages, but you're actually returning more than you think! The listboxes return content as well. In fact, anything that isn't assigned to a variable or otherwise has its output nullified will reach the output stream. To fix this, try casting listbox to [void]
like so:
[void] $listBox1.Items.Add("Testing fields")
It probably wouldn't hurt to review this TechNet guide on proper usage of Listboxes in the context of a form, either.
Upvotes: 6