NiewBiee2020 NiewBiee2020 - 1 year ago 88 Question

Option Strict On prohibits operands type object

I've got this error on my code

Option Strict On prohibits operands type object operator '<'

I want to change the row color of DataGridView to serves as warning if product quantity is < 100

here's my code.

Dim drv As DataRowView
Dim ds As DataSet
If e.RowIndex >= 0 Then
If e.RowIndex <= ds.Tables("tbl_Stocks").Rows.Count - 1 Then
drv = ds.Tables("tbl_Stocks").DefaultView.Item(e.RowIndex)
Dim c As Color

If drv.Item("Quantity") < 100 Then
c = Color.LightBlue
c = Color.Pink
End If
e.CellStyle.BackColor = c
End If
End If
End Sub

the error was thrown to
If drv.Item("Quantity") < 100

Answer Source

You can get value of needed type through Row member which is DataRow type

Dim value As Integer = drv.Row.Field(Of Integer)("Quantity")

Extension method Field(Of T)("columnName") will cast value to the type you need. And it will be tiny tiny faster then other casting methods

When using Option Strict On you will get faster feedback by compiler about possible mistakes.