Solved

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

Posted on 2014-01-27
2
219 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now