Solved

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

Posted on 2014-01-27
2
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
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!
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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