Chris Fuller  

Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity.

Posted by Chris Fuller
Monday, June 29, 2009

I was recently tasked with upgrading a client's web site, which is written in VB.NET, from VS2003 to VS2008. The conversion went pretty smoothly, but I thought I would share one of the hiccups I encountered:

One of the case statements, that apparently compiled in VS2003, did not compile in VS2008:

This is the statement:

Select Case dr.Item("DataTypeId")
Case 1, 3, 4
_dt.Columns.Add(CType(dr.Item("SystemName"), String), Type.GetType("System.String"))
Case Else
_dt.Columns.Add(CType(dr.Item("SystemName"), String), Type.GetType("System.Double"))
End Select

This is the error:

Error 105 Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity.

I thought hte problem was in the Case 1, 3, 4 syntax - I usually program in C#, and my VB is a little rusty...but after a little digging I realized that this syntax was correct and the problem had to do with the data type of the value in the select clause (object) not matching the Case statment (int).

To resolve the issue, I converted the selected clause to an integer:


Select Case Convert.ToInt32(dr.Item("DataTypeId"))
Case 1, 3, 4
_dt.Columns.Add(CType(dr.Item("SystemName"), String), Type.GetType("System.String"))
Case Else
_dt.Columns.Add(CType(dr.Item("SystemName"), String), Type.GetType("System.Double"))
End Select