首页 > 解决方案 > 数组中的元素可以引用数组吗?

问题描述

如果我有一个名为的数组standard并且我想要那个数组的计数我会做standard.Count

如果我要string调用一个数组doorType并将其添加string standard到该数组的第一个位置,但我想参考standard.Count有没有一种方法可以操作doorType[0].Count,以便它获取standard数组中的元素数?

public static List<string> doorType = new List<string> 
{
    nameof(standard),
    nameof(original)
};

public static List<string> standard= new List<string>
{
    "1",
    "4",
    "6"
};

我怎样才能使第二行相等?

string xform = doorType[0]  + standard.Count;

string xform = doorType[0]  + doorType[0].Count;

一些额外有用的代码

        public static void AddDropDown(string cellInsert, string formula)
        {

            var cell = x.Range[cellInsert];
            cell.Validation.Delete();
            cell.Validation.Add(
               Excel.XlDVType.xlValidateList,
               Excel.XlDVAlertStyle.xlValidAlertInformation,
               Excel.XlFormatConditionOperator.xlBetween,
               formula,
               Type.Missing);
            cell.Validation.IgnoreBlank = true;
            cell.Validation.InCellDropdown = true;
            cell.Value = "Please Select";



        }//end AddDropDown



string xform =
                "=IF(B2=" + DO.quote + doorType[0] + DO.quote + "," + "E1:E" + standard.Count +
                ",IF(B2=" + DO.quote + doorType[1] + DO.quote + "," + "F1: F" + original.Count +
                "," + "I1: I3" + "))))";
            AddDropDown("B4", xform);

为了更清楚地解释,我想xform使用以前制作的数组通过 for 循环将字符串附加到。

这都是为了将​​ C# 链接到 excel

标签: c#arrays

解决方案


与 Sweeper 的评论相同,或者您可以创建一个类 DoorType?

public class DoorType
{
   public List<string> DoorTypes { get; set; }
}

然后在您的标准列表中:

List<DoorType> standardList = new List<DoorType>(); 

然后您可以将每个 DoorType 添加到您的标准列表中。

standardList.Add(doorType); // make sure the doorType object and doorType.DoorTypes exists.

// refer to any items
standardList[0].DoorTypes[x] 

只是一种方式。


推荐阅读