[Solved] How do I access list inside an object [closed]


OP, this code makes me want to weep for little children, but the reason you’re getting errors is because you’re placing 4 separate variables in MessageBox.Show() call and not tying them together.

Update

Based on your comment,

when I try to type oH_N1_N1.ListH_N1_N2, there is no N201_Name01 and N201_Name02

That’s because oH_N1_N1.ListH_N1_N2 is a List<H_N1_N2> property. You cannot access properties of H_N1_N2 that way. You have to access via the list, for example using the indexer:

oH_N1_N1.ListH_N1_N2[0].N201_Name01

You can also do foreach to get all of the elements…

string crazyNames = string.Empty;
foreach(var crazyName in oH_N1_N1.ListH_N1_N2)
{
    crazyNames += crazyName.N201_Name01 + " " + N201_Name02 // etc.
}

Notice the [0] above which is the first element in the list. Now, the intellisense will show you available properties of the stored object, which is an instance of H_N1_N2 and will contain the property N201_Name01 and so on.


Original issue / answer:

Add + signs there and it’ll work.

For the love of humanity please use some different naming conventions!

foreach (var oH_N1_N1 in objH_N1_N1)
{   
    MessageBox.Show(
        // Print N1
        oH_N1_N1.N101_EntityIdentifierCode
        + "\n" + oH_N1_N1.N102_Name
        + "\n" + oH_N1_N1.N103_IdentificationCodeQualifier
        + "\n" + oH_N1_N1.N104_IdentificationCode

            + // concatenate next object to first one 

        // Print N2
        oH_N1_N1.ListH_N1_N2.N201_Name01 //ERROR HERE
        +"\n" + oH_N1_N1.ListH_N1_N2.N201_Name02 //ERROR HERE

            + // concatenate next object to first+second one 

        // Print N3
        oH_N1_N1.ListH_N1_N3.N301_AddressInformation01 //ERROR HERE
        +"\n" + oH_N1_N1.ListH_N1_N3.N301_AddressInformation02 //ERROR HERE

            + // concatenate last object to first+second+third one 

        // Print N4
        oH_N1_N1.ListH_N1_N4.N401_CityName //ERROR HERE
        +"\n" + oH_N1_N1.ListH_N1_N4.N402_StateProvinceCode //ERROR HERE
        +"\n" + oH_N1_N1.ListH_N1_N4.N403_PostalCode //ERROR HERE
        +"\n" + oH_N1_N1.ListH_N1_N4.N404_CountryCode //ERROR HERE
    );
}

9

solved How do I access list inside an object [closed]