Click here to Skip to main content
11,798,866 members (46,021 online)
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 15-Aug-11 23:05pm

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 CPallini 465
1 OriginalGriff 378
2 Maciej Los 305
3 Richard MacCutchan 281
4 phil.o 244
0 OriginalGriff 2,270
1 Maciej Los 1,810
2 KrunalRohit 1,496
3 CPallini 1,425
4 Richard MacCutchan 1,109

Advertise | Privacy | Mobile
Web01 | 2.8.151002.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