tmighty
tmighty

Reputation: 11409

VB.NET Operator = not defined

I have 2 radio buttons, and I can't check which one is called:

The error I am getting is "Operator = for type RadioButton and type RadioButton is not defined".

This is the sub in which the error is raised:

Private Sub optSwitch_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles optSwitch_1.CheckedChanged, optSwitch_0.CheckedChanged

    If sender.Checked Then

        If isInitializingComponent Then
            Exit Sub
        End If

        Dim bSwitchChecked As Boolean = sender = Me.optSwitch_1 'here the error is raised

        Me.btnSwitchConfig.Enabled = bSwitchChecked
    End If

End Sub

Upvotes: 1

Views: 569

Answers (4)

jmcilhinney
jmcilhinney

Reputation: 54457

= is for value equality. Referential equality is determine by the Is operator: Dim bSwitchChecked As Boolean = (sender Is Me.optSwitch_1)

By the way, you should turn Option Strict On because you shouldn't be doing things like this: If sender.Checked Then. sender is type Object and the Object class doesn't have a Checked property. You're relying on late-binding and that should not be done except when required. You should be casting the sender as type RadioButton if you want to access members of that type and Option Strict On will enforce that.

Upvotes: 1

spajce
spajce

Reputation: 7092

I believe that you want to get the value of sender and optSwitch_1 as boolean,

Just use the properties .Checked of optSwitch_1 and sender to get that value and avoid that error.

    Private Sub optSwitch_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        If sender.Checked Then

            Dim bSwitchChecked As Boolean = 
sender.Checked = Me.optSwitch_1.Checked 'here the error is raised

        End If
    End Sub

Upvotes: 0

Panu Oksala
Panu Oksala

Reputation: 3438

You should put option strict on from project settings to avoid runtime casting problems. The line which is causing problem does not make any sense.

Maybe this code will help you:

If CType(sender, RadioButton).Name.Equals(Me.optSwitch_1).Name AndAlso Not isInitializingComponent Then
Me.btnSwitchConfig.Enabled = sender.Checked
Else
' Add else because otherwise enabled state will always stay true.
Me.btnSwitchConfig.Enabled = False
End If

Upvotes: 0

har07
har07

Reputation: 89325

If you meant to compare 2 objects reference equality (to check if sender references the same instance as Me.optSwitch_1), try to use .Equals() method instead :

Dim bSwitchChecked As Boolean = sender.Equals(Me.optSwitch_1)

Upvotes: 0

Related Questions