Click here to Skip to main content
11,924,487 members (52,612 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
I have a scenario which is an object which has a list of usage profiles, which has a list of activities, which has a field called equipment. how do i access equipment from scenario?
Posted 16-Aug-11 0:05am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Equipment myEquipment = myProfiles["my name"].Activities["my activity"].Equipment;
However, it would be a good idea to check for null values at each stage.

"but equipment is a string. and how do i check for nulls?"

Well, you could change Equipment to string...
string myEquipment = myProfiles["my name"].Activities["my activity"].Equipment;

Checking for nulls just means that when ever you access a List, you make sure that what was looked for exists: if it isn't, then it will return a null value. If you try to use that to access another list, your will get an "object not set to an instance..." error.
string equipment = "none";
Profile p = myProfiles[0];
if (p != null)
    Activity a = p.Activities[0];
    if (a != null)
        equipment = a.Equipment;
If you want to use the string index version I suggested at the top, you will need to use a Dictionary, rather than a List.
adnama 16-Aug-11 5:16am
but equipment is a string. and how do i check for nulls?
BobJanova 16-Aug-11 5:39am
I think this is one of those times where you need to learn the basics of the language before trying to tackle complex problems.
OriginalGriff 16-Aug-11 5:40am
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151125.3 | Last Updated 16 Aug 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100