2

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
  }
}
5
  • Did you try returning the value earlier? i.e. not writing $empty= $true but directly return $true instead? Apart from that it is not that easy to help you debug this if we do not have the parts where $txtPrtName, $txtPrtIP and $listBox1 get assigned/declared. I would also recommend you not to process global variables in a function but make them parameters of that function. Commented Sep 20, 2013 at 7:35
  • thanks , i've tryed and it's the same behavior. Commented Sep 20, 2013 at 7:40
  • But it works fine like this : Commented Sep 20, 2013 at 7:41
  • function TestEmptyFields() { if($txtPrtName.get_text()-eq "") { return $true }elseif($txtPrtIP.get_text() -eq "") { return $true }else{ return $false } } Commented Sep 20, 2013 at 7:42
  • can you include the code that sets up the variables that you are testing in your function? or make them parameters? Commented Sep 20, 2013 at 7:46

1 Answer 1

6

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.

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

2 Comments

Plug, if you don't mind - stacktoheap.com/blog/2013/06/15/…
"anything that isn't assigned to a variable or otherwise has its output nullified will reach the output stream". Is this concept a Microsoft design decision for some known reason? It seems to me more an annoyance, prone to develop buggy functions.

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.