Incorrect behavior of IIf in VB.NET (.NET Framework 1.1 SP1)

A

Amit Chaudhary

I have written the following code in VB.NET
______________________________________________________

Private Sub SetSortAttributes()
If RequestMode <> 2 Then
If dgInnerGrid.Columns.Count = 0 Then
dgInnerGrid.DisplayLayout.AllowSortingDefault =
AllowSorting.No
Else
If IsPersistedState Then
If dgInnerGrid.Rows.Count >= PersistedRecordNumber
AndAlso PersistedRecordNumber > -1 AndAlso RequestMode <> 0 Then
dgInnerGrid.DisplayLayout.ActiveRow =
dgInnerGrid.Rows(Me.PersistedRecordNumber)
End If
End If
Dim sorter As String = CStr(IIf(IsPersistedState AndAlso
(RequestMode = 3 OrElse RequestMode = 0), PersistedSortColumn,
SortColumn))
Dim order As String = CStr(IIf(IsPersistedState AndAlso
(RequestMode = 3 OrElse RequestMode = 0), PersistedSortOrder,
SortOrder))
If sorter.Length > 0 AndAlso order.Length > 0 Then
ClearSortIndicator()
If order.Equals(ASC_ORDER) Then
dgInnerGrid.Columns.FromKey(sorter).SortIndicator =
Infragistics.WebUI.UltraWebGrid.SortIndicator.Ascending
Else
dgInnerGrid.Columns.FromKey(sorter).SortIndicator =
Infragistics.WebUI.UltraWebGrid.SortIndicator.Descending
End If
End If

End If
End If
End Sub

It is working perfectly fine in DEBUG Mode.
But "Object reference not set to an instance of an object." exception
in RELEASE Mode.

When I decompile the above code using Reflector.

When code is compiled in Debug Mode then decompiled Output is:
______________________________________________________

Private Sub SetSortAttributes()
If (Me.RequestMode <> 2) Then
If (Me.dgInnerGrid.Columns.Count = 0) Then
Me.dgInnerGrid.DisplayLayout.AllowSortingDefault =
AllowSorting.No
Else
If (Me.IsPersistedState AndAlso
(((Me.dgInnerGrid.Rows.Count >= Me.PersistedRecordNumber) AndAlso
(Me.PersistedRecordNumber > -1)) AndAlso (Me.RequestMode <> 0))) Then
Me.dgInnerGrid.DisplayLayout.ActiveRow =
Me.dgInnerGrid.Rows.Item(Me.PersistedRecordNumber)
End If
Dim text2 As String =
StringType.FromObject(Interaction.IIf((Me.IsPersistedState AndAlso
((Me.RequestMode = 3) OrElse (Me.RequestMode = 0))),
Me.PersistedSortColumn, Me.SortColumn))
Dim text1 As String =
StringType.FromObject(Interaction.IIf((Me.IsPersistedState AndAlso
((Me.RequestMode = 3) OrElse (Me.RequestMode = 0))),
Me.PersistedSortOrder, Me.SortOrder))
If ((text2.Length > 0) AndAlso (text1.Length > 0))
Then
Me.ClearSortIndicator
If text1.Equals("ASC") Then

Me.dgInnerGrid.Columns.FromKey(text2).SortIndicator =
SortIndicator.Ascending
Else

Me.dgInnerGrid.Columns.FromKey(text2).SortIndicator =
SortIndicator.Descending
End If
End If
End If
End If
End Sub

When code is compiled in Release Mode then decompiled Output is:
______________________________________________________

Private Sub SetSortAttributes()
If (Me.RequestMode <> 2) Then
If (Me.dgInnerGrid.Columns.Count = 0) Then
Me.dgInnerGrid.DisplayLayout.AllowSortingDefault =
AllowSorting.No
Else
If ((Me.IsPersistedState AndAlso
(Me.dgInnerGrid.Rows.Count >= Me.PersistedRecordNumber)) AndAlso
((Me.PersistedRecordNumber > -1) AndAlso (Me.RequestMode <> 0))) Then
Me.dgInnerGrid.DisplayLayout.ActiveRow =
Me.dgInnerGrid.Rows.Item(Me.PersistedRecordNumber)
End If
Dim text2 As String =
StringType.FromObject(Interaction.IIf((Me.IsPersistedState AndAlso
((Me.RequestMode = 3) OrElse (Me.RequestMode = 0))),
Me.PersistedSortColumn, Me.SortColumn))
Dim text1 As String =
StringType.FromObject(Interaction.IIf((Me.IsPersistedState AndAlso
((Me.RequestMode = 3) OrElse (Me.RequestMode = 0))),
Me.PersistedSortOrder, Me.SortOrder))
If ((text2.Length > 0) AndAlso (text1.Length > 0))
Then
Me.ClearSortIndicator
If text1.Equals("ASC") Then

Me.dgInnerGrid.Columns.FromKey(text2).SortIndicator =
SortIndicator.Ascending
Else

Me.dgInnerGrid.Columns.FromKey(text2).SortIndicator =
SortIndicator.Descending
End If
End If
End If
End If
End Sub
______________________________________________________

The statement in Debug Mode:

If (Me.IsPersistedState AndAlso (((Me.dgInnerGrid.Rows.Count >=
Me.PersistedRecordNumber) AndAlso (Me.PersistedRecordNumber > -1))
AndAlso (Me.RequestMode <> 0))) Then

The statement in Release Mode:

If ((Me.IsPersistedState AndAlso (Me.dgInnerGrid.Rows.Count >=
Me.PersistedRecordNumber)) AndAlso ((Me.PersistedRecordNumber > -1)
AndAlso (Me.RequestMode <> 0))) Then

Is the root of the exception? I can implement the workaround.
But Is it possible for any Microsoft guys to confirm the problem.

-Amit
 
P

Phill W.

Amit said:
Dim sorter As String = CStr(IIf(IsPersistedState AndAlso
(RequestMode = 3 OrElse RequestMode = 0), PersistedSortColumn,
SortColumn))

OK, it /could/ be a Bug but, IMHO, the way you've coded this feels wrong.

IIf /always/ evaluates all three of its arguments, regardless of the
outcome of the first.
AndAlso and OrElse are /shortcut/ operators, that drop-out as soon as
they see something they don't like.

It just feels to me like the two shouldn't be mixed.

I would suggest expanding them into full-blown If's:

Dim sorter As String = CStr( SortColumn )
If IsPersistedState _
AndAlso (RequestMode = 3 OrElse RequestMode = 0) _
Then
sorter = CStr( PersistedSortColumn )
End If

HTH,
Phill W.
 
G

Guest

IIf is not suited to direct logic flow - it is just a function and as such
all arguments are always fully evaluated.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top