There are several things which can be improved in that code. First and foremost:
Convert.ToInt32(grpDiscount.Controls)
That code tries to convert an entire controls collection to an integer, which cannot ever work. As a result, the Exception is always thrown.
You apparently want to know which control is checked, but that alone will not tell you the Discount level desired: a control which says "2% Discount" is not the same a 2
. A UI Control will tell you what the user wants, but you need some way to convert that back into something like a number.
Determine checked RB
Dim rb = GroupBox3.Controls.OfType(Of RadioButton).
FirstOrDefault(Function(w) w.Checked)
This will tell you which, if any, control is checked. If each control had the discount stored in the .Tag
property (e.g. the "10% Discount" control had 10
), you could get the amount back from the Checked one:
If rb IsNot Nothing Then
' determine discount amount
DiscountLevel = Convert.ToInt32(rb.Tag)
End If
Return DiscountLevel
If none are selected, then rb
will be Nothing
. That ought not ever happen - a group of RadioButtons
should provide for a default.
Loop version
Dim DiscountLevel As Int32 = -1
For Each rbtn As RadioButton In GroupBox3.Controls.OfType(Of RadioButton)()
If rbtn.Checked Then
DiscountLevel = Convert.ToInt32(rb.Tag)
End If
Next
At the end, if one is checked, DiscountLevel
will be >-1.
Exceptions
Exceptions are for exceptional situations, not flow control or data validation. Your code is actually chastising the user when your code fails. That code is not so complex or error prone that it needs a Try/Catch.
ComboBox
A ComboBox
could do the same thing as a group of RadioButtons
, but do so better and take less space. You could populate it with DiscountLevel
objects, where the text says "10%" or whatever, while returning a numeric value to your code.
Basic class to keep a name/text and value together. This will display the name/text to the user, but return a value to your code.