1

Ok, I asked this question and got excellent code example as answer. The code works but I don't understand the meaning of the code. Can someone point the direction for me for further reading in order to understand the code. Here is the code that retrieves the checked radio button in a groupbox:

Dim rButton As RadioButton = GroupBox1.Controls _
    .OfType(Of RadioButton)() _
    .Where(Function(r) r.Checked = True) _
    .FirstOrDefault()

Ok, the parts that I don't understand are .OfType, .Where, .FirsrOrDefault

UPDATE:

Thanks guys, those things are LINQ

Community
  • 1
  • 1
Predator
  • 1,267
  • 3
  • 17
  • 43
  • In your previous thread you asked how to replace a bunch of if's and it looks like you have selected. But what is the point? Now you know which one is checked, in a different way, and now... – dbasnett Jun 24 '11 at 16:48
  • @dbasnett: My previous question purpose is to explore if there is a smarter way to check which radio button being checked. And I tried to use the code example given by the answerer. It works and thank-you very much. But I don't understand the code. Specifically I even don't know what is `.OfType` and where it comes from. So, I started this question. Anything wrong from my part? – Predator Jun 24 '11 at 17:02
  • No. My point was now that you know which button is checked what are you going to do. – dbasnett Jun 24 '11 at 17:39
  • @dbasnett: I'm going to ask another question. Here is the new question http://stackoverflow.com/questions/6471695/how-to-calculate-the-total-time-a-user-spending-on-an-application – Predator Jun 24 '11 at 19:09

4 Answers4

7

The code almost reads exactly what it is doing: from the controls on GroupBox1 that are of type RadioButton, take those where the radio button is checked, and then take the first one (or null if there aren't any).

In plainer English, among all the radio buttons in the group box, find the first checked one, or return null if there aren't any.

The methods come from LINQ.

jason
  • 236,483
  • 35
  • 423
  • 525
  • I mean what is the name/tech of following things: `.OfType, .Where, .FirsrOrDefault` ? I know nothing about those thing and wish to learn it. Can you point to me where to learn more? – Predator Jun 24 '11 at 16:29
  • @Predator: These are LINQ methods. LINQ is what you want to read about. – jason Jun 24 '11 at 16:30
5

This code selects the first checked radio button in a group of buttons. Lets walk through the code:

  1. Dim rButton As RadioButton = GroupBox1.Controls _

    Select the group of form controls

  2. OfType(Of RadioButton)() _

    But only the Radio buttons from that group

  3. Where(Function(r) r.Checked = True) _

    That are already checked

  4. .FirstOrDefault()

    Return the first one or NULL if none are checked.

Swift
  • 13,118
  • 5
  • 56
  • 80
  • Ok, I pick this as answer because it has detailed explanation. I have upvoted all other answers. Many thanks! – Predator Jun 24 '11 at 16:41
2

This is the LINQ API for VB. Basically, each of the methods you mention are selectors and are returning the results of a query. Check out this page for a ton of examples:

AdamC
  • 16,087
  • 8
  • 51
  • 67
1

Basically, its going through the controls in GroupBox1 that are OfType radiobutton, and Where they are checked, grabbing the First result or Default/None if there are no results.

Jordan Foreman
  • 3,848
  • 7
  • 40
  • 65