We help IT Professionals succeed at work.
Get Started

C# - getting associated values from a class

skij
skij asked
on
183 Views
Last Modified: 2015-03-25
Using C#, in the example below, how can I get the associated age by a name?

For example, I want to print the age of the Cat named Whiskers.

using System;
using System.Linq;
using cAlgo.API;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot()]
    public class DemoExample : Robot
    {
        List<Cat> myCats = new List<Cat> 
        {
            new Cat 
            {
                Name = "Sylvester",
                Age = 8,
                LuckyNumbers = {1,2,3,4,5}
            },
            new Cat 
            {
                Name = "Whiskers",
                Age = 2,
                LuckyNumbers = {22,11,3,5,2}

            },
            new Cat 
            {
                Name = "Sasha",
                Age = 13,
                LuckyNumbers = {122,1,3,25,1}
            }
        };


        protected override void OnStart()
        {

            Print("The age of Whiskers is: " + myCats);

        }

        protected override void OnTick()
        {

        }

    }
}

public class Cat
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<double> LuckyNumbers = new List<double>();
}

Open in new window

Comment
Watch Question
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
Unlock 3 Answers and 6 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE