Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# list
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 at 16-Aug-11 5:16am
but equipment is a string. and how do i check for nulls?
BobJanova at 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 at 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
0 Sergey Alexandrovich Kryukov 7,442
1 OriginalGriff 5,996
2 Peter Leow 4,292
3 CHill60 2,858
4 Mika Wendelius 2,850

Advertise | Privacy | Mobile
Web03 | 2.8.150224.1 | 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