指定网格值的取值范围 VB.NET
Specifying Value Range for Grid Value VB.NET
如何避免收到此警告?如果警告和代码保持如下,软件会抛出 运行 时间错误吗?编写此代码的更好方法是什么?由于我不能对网格值使用最小值和最大值,因此我只能使用 .Value,因此我编写了以下代码。
Select Case CSng(dgv_config.Item(dgv_config.Columns("p").Index, rowindex).Value)
Case 1 To 150
#Disable Warning BC42019 ' Operands of type Object used for operator
If dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value > 400 Then
#Enable Warning BC42019 ' Operands of type Object used for operator
dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value = 400
End If
#Disable Warning BC42019 ' Operands of type Object used for operator
If dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value < 50 Then
#Enable Warning BC42019 ' Operands of type Object used for operator
dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value = 50
End If
End Select
Hursey 的评论是正确的,类型转换会删除警告。
为了解释,我将重写您的一些代码以使其更清晰,从此...
If dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value > 400 Then
dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value = 400
End If
...为此:
Dim columnIndex As Integer = dgv_config.Columns("tsamp").Index
Dim cellValue As Object = dgv_config.Item(columnIndex, rowindex).Value
If cellValue > 400 Then
dgv_config.Item(columnIndex, rowindex).Value = 400
End If
注意 cellValue
是对象类型。那是因为DataGridViewCell.Value
属性returns一个对象。这样做的原因是因为单元格中可以有不同类型的内容,例如字符串或整数。您的列将包含一个整数,但编译器不知道。
在我的 Visual Studio 上面的代码片段中显示了您遇到的警告:
BC42019: Operands of type Object used for operator '<'; runtime errors could occur.
为了消除警告,我们添加了类型转换,例如 Cint()
:
If CInt(dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value) > 400 Then
或者用另一种方式告诉编译器我们需要一个整数,Integer.Parse()
。
If Integer.Parse(dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value) > 400 Then
顺便说一下,直到我在项目设置 - 编译页面上启用 'Late binding; call could fail at run time' 警告配置后,警告才出现在我的 Visual Studio 中。
如何避免收到此警告?如果警告和代码保持如下,软件会抛出 运行 时间错误吗?编写此代码的更好方法是什么?由于我不能对网格值使用最小值和最大值,因此我只能使用 .Value,因此我编写了以下代码。
Select Case CSng(dgv_config.Item(dgv_config.Columns("p").Index, rowindex).Value)
Case 1 To 150
#Disable Warning BC42019 ' Operands of type Object used for operator
If dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value > 400 Then
#Enable Warning BC42019 ' Operands of type Object used for operator
dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value = 400
End If
#Disable Warning BC42019 ' Operands of type Object used for operator
If dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value < 50 Then
#Enable Warning BC42019 ' Operands of type Object used for operator
dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value = 50
End If
End Select
Hursey 的评论是正确的,类型转换会删除警告。
为了解释,我将重写您的一些代码以使其更清晰,从此...
If dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value > 400 Then
dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value = 400
End If
...为此:
Dim columnIndex As Integer = dgv_config.Columns("tsamp").Index
Dim cellValue As Object = dgv_config.Item(columnIndex, rowindex).Value
If cellValue > 400 Then
dgv_config.Item(columnIndex, rowindex).Value = 400
End If
注意 cellValue
是对象类型。那是因为DataGridViewCell.Value
属性returns一个对象。这样做的原因是因为单元格中可以有不同类型的内容,例如字符串或整数。您的列将包含一个整数,但编译器不知道。
在我的 Visual Studio 上面的代码片段中显示了您遇到的警告:
BC42019: Operands of type Object used for operator '<'; runtime errors could occur.
为了消除警告,我们添加了类型转换,例如 Cint()
:
If CInt(dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value) > 400 Then
或者用另一种方式告诉编译器我们需要一个整数,Integer.Parse()
。
If Integer.Parse(dgv_config.Item(dgv_config.Columns("tsamp").Index, rowindex).Value) > 400 Then
顺便说一下,直到我在项目设置 - 编译页面上启用 'Late binding; call could fail at run time' 警告配置后,警告才出现在我的 Visual Studio 中。