Hi James,
I used the tag method but had to change it slightly because VS 2008 with
.NET Framework 3.5 moaned that the return value could be NULL
Private Function GetIndex() As String
Dim intTemp As Integer = 0
For i As Integer = 0 To GroupBox1.Controls.Count - 1
If TypeOf (GroupBox1.Controls(i)) Is RadioButton Then
If CType(GroupBox1.Controls(i), RadioButton).Checked Then
intTemp = CType(GroupBox1.Controls(i), RadioButton).Tag
End If
End If
Next
Return intTemp
End Function
Thanks for your time & effort in replying!1
What the error is really telling you is that you have a way out of the
function (if the For loop doesn't find a match) that doesn't
explicitly return a value:
"Function 'GetIndex' doesn't return a value on all code paths. A null
reference exception could occur at turn time when the result is used."
You could have fixed the error by putting a 'Return ""' at then end of
the routine, rather than introducing unnecessary conversions between
Strings and Integers.
A bigger problem is that you are not using Option Strict On. Your
final code has an implicit conversion from Object to Integer, and
another from Integer to String, which is inefficient. Assuming the
Tag fields contain strings, I would do it this way:
Private Function GetIndex() As String
For i As Integer = 0 To GroupBox1.Controls.Count - 1
If TypeOf (GroupBox1.Controls(i)) Is RadioButton Then
Dim rb As RadioButton = DirectCast(GroupBox1.Controls(i),
RadioButton)
If rb.Checked Then
Return CType(rb.Tag, String)
End If
End If
Next
Return "" ' Or Return Nothing
End Function
If you really want an Integer value for which button is checked, then
you should store Integers in the Tag properties, change the function
to return an Integer, and CType the Tag field to Integer. That way
you won't be doing unnecessary conversions from String to Integer in
your code.