Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

Try:
C#
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...
C#
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.
C#
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.
 
Share this answer
 
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900