Tony Gardner Tony Gardner - 1 year ago 46
Vb.net Question

Why doesn't IIf work the same as If/Then/EndIf?

Can someone help me to understand why If/Then/End If works but IIf doesn't?

I ran my code with this and it works as expected:

For Each BtnObj In Padre.Controls
With BtnObj
If nullinator(.Tag) = 0 Then
.Visible = False
Else
.Visible = True
End If
End With
Next


Here's my "nullinator" function:

Public Shared Function nullinator(ByVal CheckVal As String) As Integer
' Receives a string and returns an integer (zero if Null or Empty or original value)
If String.IsNullOrEmpty(CheckVal) Then
Return 0
Else
Return CheckVal
End If
End Function


However, if I run it using this code, nullinator(.Tag) = 0 always equates to be False even if the contents of Tag property is empty. Here's the code:

For Each BtnObj In Padre.Controls
With BtnObj
IIf(nullinator(.Tag) = 0, .Visible = False, .Visible = True)
End With
Next


Oh yes, the "Padre" variable is the container (parent) object.

Any insights why I'm not having success using IIf would be greatly appreciated!

Answer Source

IIf returns a value:

.Visible = IIf(nullinator(.Tag) = 0, False, True)

Returns one of two objects, depending on the evaluation of an expression.

https://msdn.microsoft.com/en-us/library/27ydhh0d(v=vs.90).aspx

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download