Click here to Skip to main content
Rate this: bad
good
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
adnama424

1 solution

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

Solution 1

Try:
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.
  Permalink  
v2
Comments
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 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 16 Aug 2011
Copyright © CodeProject, 1999-2014
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