如何访问对象内的列表

时间:2014-02-14 04:04:44

标签: c# list oop

如何读取从H_N1_N1调用的H_N1_N2,H_N1_N3,H_N1_N4内的值。

我的目标是将所有值放入包含其他对象的List中。

要求是在验证之前消除将值存储到数据库中的临时表的当前进程。验证后,它会进入物理表。

这是我的实体:

public class H_N1_N1
{
    public string N101_EntityIdentifierCode { get; set; }
    public string N102_Name { get; set; }
    public string N103_IdentificationCodeQualifier { get; set; }
    public string N104_IdentificationCode { get; set; }

    public H_N1_N1()
    {
        ListH_N1_N2 = new List<H_N1_N2>();
        ListH_N1_N3 = new List<H_N1_N3>();
        ListH_N1_N4 = new List<H_N1_N4>();
    }

    public List<H_N1_N2> ListH_N1_N2 { get; set; }
    public List<H_N1_N3> ListH_N1_N3 { get; set; }
    public List<H_N1_N4> ListH_N1_N4 { get; set; }
}

public class H_N1_N2
{
    public string N201_Name01 { get; set; }
    public string N202_Name02 { get; set; }
}

public class H_N1_N3
{
    public string N301_AddressInformation01 { get; set; }
    public string N302_AddressInformation02 { get; set; }
}

public class H_N1_N4
{
    public string N401_CityName { get; set; }
    public string N402_StateProvinceCode { get; set; }
    public string N403_PostalCode { get; set; }
    public string N404_CountryCode { get; set; }
}

以下是我填充对象的方法:

var N1_N1 = dal.GetModelDetails(11);
for (int i = 0; i < Convert.ToInt16(N1_N1.MaxLoop); i++)
{
    H_N1_N1 oH_N1_N1 = new H_N1_N1();
    stElements = ResizeArray(stElements.Count(), stElements, "H_N1_N1");
    oH_N1_N1.N101_EntityIdentifierCode = stElements[1];
    oH_N1_N1.N102_Name = stElements[2];
    oH_N1_N1.N103_IdentificationCodeQualifier = stElements[3];
    oH_N1_N1.N104_IdentificationCode = stElements[4];
    objH_N1_N1.Add(oH_N1_N1);

    var N1_N2 = dal.GetModelDetails(12);
    H_N1_N2 oH_N1_N2 = new H_N1_N2();
    stElements = ResizeArray(stElements.Count(), stElements, "H_N1_N2");
    oH_N1_N2.N201_Name01 = stElements[1];
    oH_N1_N2.N202_Name02 = stElements[2];
    oH_N1_N1.ListH_N1_N2.Add(oH_N1_N2);

    var N1_N3 = dal.GetModelDetails(14);
    H_N1_N3 oH_N1_N3 = new H_N1_N3();
    stElements = ResizeArray(stElements.Count(), stElements, "H_N1_N3");
    oH_N1_N3.N301_AddressInformation01 = stElements[1];
    oH_N1_N3.N302_AddressInformation02 = stElements[2];
    oH_N1_N1.ListH_N1_N3.Add(oH_N1_N3);

    var N1_N4 = dal.GetModelDetails(16);
    H_N1_N4 oH_N1_N4 = new H_N1_N4();
    stElements = ResizeArray(stElements.Count(), stElements, "H_N1_N4");
    oH_N1_N4.N401_CityName = stElements[1];
    oH_N1_N4.N402_StateProvinceCode = stElements[2];
    oH_N1_N4.N403_PostalCode = stElements[3];
    oH_N1_N4.N404_CountryCode = stElements[4];
}

我无法读取对象的部分:

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

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

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

        // 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
    );
}

这就是我想要做的: N1_somedata N1_somedata N1_somedata N1_somedata N2_somedata N2_somedata N3_somedata N3_somedata N4_somedata N4_somedata N4_somedata N4_somedata

N1_somedata N1_somedata N1_somedata N1_somedata N2_somedata N2_somedata N3_somedata N3_somedata N4_somedata N4_somedata N4_somedata N4_somedata

N1_somedata N1_somedata N1_somedata N1_somedata N2_somedata N2_somedata N3_somedata N3_somedata N4_somedata N4_somedata N4_somedata N4_somedata

提前致谢!

1 个答案:

答案 0 :(得分:1)

OP,这段代码让我想为小孩子哭泣,但你得到错误的原因是因为你在MessageBox.Show()电话中放置了4个单独的变量,而不是将它们绑在一起。

<强>更新

根据您的评论,

  

当我尝试输入oH_N1_N1.ListH_N1_N2时,没有N201_Name01和N201_Name02

那是因为oH_N1_N1.ListH_N1_N2List<H_N1_N2>属性。您无法以这种方式访问​​H_N1_N2的属性。您必须通过列表访问,例如使用索引器:

oH_N1_N1.ListH_N1_N2[0].N201_Name01

您也可以foreach获取所有元素......

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

注意上面的[0]是列表中的第一个元素。现在,intellisense将显示存储对象的可用属性,该属性是H_N1_N2的实例,并将包含属性N201_Name01等等。


原始问题/答案:

在那里添加+标志,它会起作用。

为了爱人类,请使用一些不同的命名约定!

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
    );
}