可以为空的参数显示不正确的值

时间:2014-03-31 03:40:31

标签: .net vb.net parameters nullable

无法理解这一点。我有一个像这样定义的方法:

Sub M1(searchText As String, companyFilter As Integer?)
    ...
    'Check the value of companyFilter HERE                                          <<<<
    ...
End Sub

我这样称呼它:

 M1(txtSearch.Text, If(cbo.SelectedIndex = 0, Nothing, cbo.SelectedValue))

cbo.SelectedIndex0。您期望companyFilter的值在突出显示的行中是什么? Nothing?我也是。但令我惊讶的是,价值是0。发生了什么事?

1 个答案:

答案 0 :(得分:2)

如果我没记错的话,VB.NET(比如C#)假定表达式应该是cbo.SelectedValue的类型。尝试将其投射到Nullable

M1(txtSearch.text,
    If(cbo.SelectedIndex = 0, Nothing, New Integer?(cbo.SelectedValue))

(......有效吗?对不起。已经有一段时间了。)并确保Option Strict On

相关问题