Solved

JerseyNumber jerseyNumber = retiredNumbers[(int)number.SelectedItem] as JerseyNumber;

Posted on 2014-01-27
2
222 Views
Last Modified: 2014-02-03
Could someone explain me this line of code?

JerseyNumber jerseyNumber = retiredNumbers[(int)number.SelectedItem] as JerseyNumber;


Here is the entire code

public partial class Form1 : Form
    {
        Dictionary<int, JerseyNumber> retiredNumbers = new Dictionary<int, JerseyNumber>() {
            {3, new JerseyNumber("Babe Ruth", 1948)},
            {4, new JerseyNumber("Lou Gehrig", 1939)},
            {5, new JerseyNumber("Joe DiMaggio", 1952)},
            {7, new JerseyNumber("Mickey Mantle", 1969)},
            {8, new JerseyNumber("Yogi Berra", 1972)},
            {10, new JerseyNumber("Phil Rizzuto", 1985)},
            {23, new JerseyNumber("Don Mattingly", 1997)},
            {42, new JerseyNumber("Jackie Robinson", 1993)},
            {44, new JerseyNumber("Reggie Jackson", 1993)},
        };

        public Form1()
        {
            InitializeComponent();

            foreach (int key in retiredNumbers.Keys)
            {
                number.Items.Add(key);
            }
        }
        private void number_SelectedIndexChanged(object sender, EventArgs e)
        {
            JerseyNumber jerseyNumber = retiredNumbers[(int)number.SelectedItem] as JerseyNumber;
            nameLabel.Text = jerseyNumber.Player;
            yearLabel.Text = jerseyNumber.YearRetired.ToString();
        }
    }

Open in new window

0
Comment
Question by:yguyon28
2 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39812838
The line of code is creating an object jerseyNumber of Type JerseyNumber that is then being set to the dictionary entry of the dictionary object retiredNumbers determined by the Selected Item of the list control number.

Essentially a list control of some type (probably dropdownlist) is being used to determine which dictionary entry in the dictionary object retiredNumbers is set to the object variable jerseyNumber.

Hope that helps
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
ID: 39812843
Presumably, "numbers" is a listbox or combobox.

There is the Dictionary retiredNumbers which is clearly visible in the code.

Items in the listbox would have values such as 3, 4, etc.

The user selects an item in that listbox, triggering a SelectedIndexChanged event.

The value of the selected item (say it is 10 for example) is looked up in the Dictionary to provide the JerseyNumber object (in the 10 example, that would be {"Phil Rizzuto", 1985}
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question