views:

35

answers:

3

I have say 4 asp.net radiobuttons which are grouped, I want to find out which one is checked.

What seems natural to do is:

Select Case RadioButton.Checked = True
        Case myRadioButton1
        Case myRadioButton2
        Case myRadioButton3
        Case Else

End Select

I just get a 'reference to a non-shared member reference' error. It's a shame because it seems such a clean way to do this test.. Is it possible??

A: 

No. The Checked property of class RadioButton is non-shared, so you cannot get its value unless you have an instance. You can do this using an ElseIf chain, but the cleanest solution IMO is to use a RadioButtonList instead, which will allow you to add items dynamically and read the selected item in one call, just like with a DropdownList.

tdammers
I need granular control over radiobutton placement. Radiobuttonlists are horrible.. They wrap the radiobutton in a <span/> and apply classes to the span not the input..
Markive
A: 

You could use a RadioButtonList instead. You can only check for primitive data-types in select/case. With normal RadioButtons you could only check for a reference in the CheckedChanged Event when all CheckBoxes are handled there. For example:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler Me.RadioButton1.CheckedChanged, AddressOf CheckedChanged
        AddHandler Me.RadioButton2.CheckedChanged, AddressOf CheckedChanged
    End Sub

    Private Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
       If sender Is Me.RadioButton1 Then

        ElseIf sender Is Me.RadioButton2 Then
            '........
        End If
    End Sub

But that was not your question anyway. Use a RadiobuttonList instead as i mentioned above or a If/else on checked-state because it is more natural.

Tim Schmelter
+5  A: 
Select Case True

  Case RadioButton1.Checked

  Case RadioButton2.Checked

  Case RadioButton3.Checked

  Case Else

End Select
fivebob
So simple can't believe I didn't think of it.. well played!
Markive