Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top