Predator
Predator

Reputation: 1275

What is the meaning of this code?

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

Upvotes: 1

Views: 245

Answers (4)

Swift
Swift

Reputation: 13188

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.

Upvotes: 5

jason
jason

Reputation: 241611

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.

Upvotes: 7

Jordan Foreman
Jordan Foreman

Reputation: 3888

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.

Upvotes: 1

AdamC
AdamC

Reputation: 16273

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:

Upvotes: 2

Related Questions