?
Solved

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

Posted on 2014-01-27
2
Medium Priority
?
233 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 1500 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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