Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to sort columns in DataGridView 4 64
Updating a single record in forEach using C#/Entity 7 39
C# DataTable (in memory) vs SQL Lookups 4 74
Put File 6 36
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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

732 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