質問

Can't get my head around this one. I have a method defined like this:

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

I'm calling it like this:

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

cbo.SelectedIndex is 0. What do you expect the value of companyFilter to be at the highlighted line? Nothing? So do I. But to my surprise the value is 0. What's going on?

役に立ちましたか?

解決

If I recall correctly, VB.NET (like C#) assumes the expression should be of cbo.SelectedValue’s type. Try casting it to a Nullable:

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

(… that works, right? Sorry. It’s been a while.) And make sure to have Option Strict On.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top