Using VB.NET If vs. IIf in binding / rendering expression
I am trying to bind UI elements to a business object exposed as a property in an ASP.NET page with zero security.
Using the "If" statement is null, but results in a compiler error:
Compiler error message: BC30201: Expected expression.
Using "IIf" succeeds, but is not null. I tried the expression expression syntax ('<% =%>') ('<% #%>') but there was no difference.
Can someone please explain this inconsistency and maybe provide an alternative?
Sample code:
This works: <% = IIf (Me.Foo Nothing, "", Me.Foo.Id)%>
This raises a compiler error: <% = If (Me.Foo Is Nothing, "", Me.Foo.Id)%>
source to share
Okay, revisiting the question, I think we can bark the wrong tree with IF (). The answer probably lies in the error message:
Compiler error message: BC30201: Expected expression.
So, I created a sample application. Standard Visual Studio 2008 Web Application. I created a class named Bar and added it to the app_code folder:
Imports Microsoft.VisualBasic
Public Class Bar
Public Id As String
End Class
On the default.aspx page, I added the following to the code file:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Foo As New Bar()
End Class
Please note what is Foo
marked protected
.
On the page, I added the following code:
<form id="form1" runat="server">
<div>
<%=If(Me.Foo Is Nothing, "", Me.Foo.Id)%>
</div>
</form>
This works for me and I am not getting any errors. If I then modify the code-behind as follows, I get the expected result ("Something" appears on a blank page):
Partial Class _Default
Inherits System.Web.UI.Page
Protected Foo As New Bar()
Public Sub New()
Foo.Id = "Something"
End Sub
End Class
If that doesn't work for you, then my suggestion for you is to check that you are targeting the .NET 3.5 Framework. When I target .NET 2.0, I get "Expression Expected" on the IF () call. This error does not occur when targeting 3.5.
You can check that you are targeting 3.5 to the Build tab of your web application properties.
source to share
Are you using VB2008? The If () statement was not available before VB versions, which may explain your compiler error.
In earlier VB versions, I would use Jason's answer .
<% if someCondition then %>
some html here
<% end if %>
source to share
When used, the IIf
expressions "true" and "false" are evaluated before calling IIf.
This is the same as calling this function:
Public Function MyIIf(ByVal test As Boolean, ByVal trueOption As Object, ByVal falseOption As Object) As Object
If test Then Return trueOption Else Return falseOption
End Function
So, before being IIf
called, it Me.Foo.Id
is evaluated and throws an exception.
This is not how the C # conditional ( ?:
) operator works . This is likely the source of the confusion.
source to share
VB.NET now includes a short-circuited IF () statement. if the first statement is incorrect, the second is NEVER evaluated. This is completely different from how IIF works, and the way any custom function works. This is actually equivalent to the C # operator ?:
The documentation can be found here: IF statement on MSDN
source to share