Search code examples
vb.netif-statementcomparison

Why can't I do comparison in VB.net?


If (cmbBloodgroup.SelectedItem == null) Then
            MessageBox.Show("Please select a blood group.")
Endif

The error 'Expression expected' is on the second equal sign. When I clicked the 'Show potential fixes', it gave me 2 options: remove unnecessary parentheses, and invert if. The first option doesn't remove the error. The second option makes it like this:

cmbBloodgroup.SelectedItem =IsNot null

But I want the condition for it to be null. Idk what I should change here. (I'm checking combobox. If it's empty, I want to show a messagebox)


Solution

  • The vb syntax is as below , what you had tried is c#

      if cmbBloodgroup.SelectedItem is Nothing Then
           MessageBox.Show("Please select a blood group.")
        End if