Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数组中的元素能否引用数组?_C#_Arrays - Fatal编程技术网

C# 数组中的元素能否引用数组?

C# 数组中的元素能否引用数组?,c#,arrays,C#,Arrays,如果我有一个名为standard的数组,并且我想要该数组的计数,我会执行standard.count 但是 如果我有一个名为doorType的string数组,我将stringstandard添加到该数组的第一个位置,但我想引用standard.Count有什么方法可以操作doorType[0].Count以便获得标准数组中的元素数 public static List<string> doorType = new List<string> { nameof(s

如果我有一个名为
standard
的数组,并且我想要该数组的计数,我会执行
standard.count

但是

如果我有一个名为
doorType
string
数组,我将
string
standard
添加到该数组的第一个位置,但我想引用
standard.Count
有什么方法可以操作
doorType[0].Count
以便获得
标准
数组中的元素数

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

public static List<string> standard= new List<string>
{
    "1",
    "4",
    "6"
};
一些额外的有用代码

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

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


这就是将C#链接到excel的全部内容,与Sweeper的注释相同,或者您可以创建一个类DoorType

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

只是一种方式。

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

public class DoorType
{
   public List<string> DoorTypes { get; set; }
}
只是一种方法。

在C#中,你不能(很容易)从对象的名称操纵对象,就是这样,你不能操纵对象
标准
(并获取其计数),仅仅因为你知道名称是字符串
“standard”

不要将对象名称放在
doorType
列表中,而是将对象的引用放在其中
doorType
将不是字符串列表,而是字符串列表。然后你可以写:

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

public static List<string> original = new List<string>
{
    "a",
    "b",
    "c"
};

public static List<List<string>> doorType = new List<List<string>>
{
    standard,
    original
};
string xform = "=IF(B2=" + DO.quote + doorTypes[0].Name + DO.quote + "," + "E1:E" + doorTypes[0].Values.Count
此时,
doorType[0]。value
是对
standard
的引用,您可以编写
doorType[0]。value.Count
以获取
standard.Count
doorType[0]。Name
是字符串
“standard”

你可以写:

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

public static List<string> original = new List<string>
{
    "a",
    "b",
    "c"
};

public static List<List<string>> doorType = new List<List<string>>
{
    standard,
    original
};
string xform = "=IF(B2=" + DO.quote + doorTypes[0].Name + DO.quote + "," + "E1:E" + doorTypes[0].Values.Count
然后在
doorTypes
集合上进行
Linq
调用或
foreach
循环


1事实上,你可以使用,但这不是一个很好的任务模式。

在C#中,你不能(很容易)从对象的名称操纵对象,就是这样,你不能操纵对象
标准
(并获取其计数),仅仅因为你知道名称是字符串
“standard”

不要将对象名称放在
doorType
列表中,而是将对象的引用放在其中
doorType
将不是字符串列表,而是字符串列表。然后你可以写:

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

public static List<string> original = new List<string>
{
    "a",
    "b",
    "c"
};

public static List<List<string>> doorType = new List<List<string>>
{
    standard,
    original
};
string xform = "=IF(B2=" + DO.quote + doorTypes[0].Name + DO.quote + "," + "E1:E" + doorTypes[0].Values.Count
此时,
doorType[0]。value
是对
standard
的引用,您可以编写
doorType[0]。value.Count
以获取
standard.Count
doorType[0]。Name
是字符串
“standard”

你可以写:

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

public static List<string> original = new List<string>
{
    "a",
    "b",
    "c"
};

public static List<List<string>> doorType = new List<List<string>>
{
    standard,
    original
};
string xform = "=IF(B2=" + DO.quote + doorTypes[0].Name + DO.quote + "," + "E1:E" + doorTypes[0].Values.Count
然后在
doorTypes
集合上进行
Linq
调用或
foreach
循环



1事实上,你可以使用,但这不是一个很好的任务模式。

如果
门类型
保持为
列表
,就没有办法做到这一点
doorType
应该是一个类似于
列表的东西
@Sweeper我开始想这个问题背后的问题是什么?没有任何上下文,这个请求看起来真的很奇怪。如果你解释你为什么要做这件事,你可能会得到更有用的反馈,并找到更好的方法来完成你的工作。@PatrickRoberts我正在将c#代码链接到Excel并制作下拉列表。我把它们和我以前做的项目联系起来。基本上,我想把它变成一个for循环
“=IF(B2=“+DO.quote+doorType[0]+DO.quote+”,“+E1:E”+StandardDoor.Count+
@PatrickRoberts我给了它一个编辑如果
doorType
作为一个
列表
doorType
应该像一个
列表
@Sweeper我开始想这个问题背后的问题是什么?这个请求看起来真的很奇怪,没有任何控制ext.如果你解释你为什么要做这个特定的事情,你可能会得到更有用的反馈,以及一个更好的方法来完成你的工作。@PatrickRoberts我正在将c代码链接到Excel并制作下拉列表。我正在将它们链接到我以前做过的项目。基本上我想把它变成一个for循环
“=If(B2=”+DO.quote+doorType[0]+DO.quote+”,“+”E1:E“+StandardDoor.Count+
@PatrickRoberts如果我使用
string.Join(“,”,doorType.ToArray()),我已经给了它一个编辑器
对于一个普通的列表,我如何操作它以用于新的
列表
?@Joelad,我编辑了我的答案,但我不会再进一步了,因为在这一点上,它刚刚开始是一个“当我来自excel-101时如何进行POO”;-)@Joelad,如果您只使用
value
Count
属性,那么用
int Count
替换
List value
,并调用
new DoorType(standard.Count,nameof(standard))
我在
var arrayL1=string.Join(,”,doorTypes.Select(p=>p.Name))之前得到了查询的答案;AddDropDown(“B2”,arrayL1);
如果我使用的是
string.Join(“,”,doorType.ToArray());
对于一个普通的列表,我如何处理它以用于新的
列表呢?@Joelad,我编辑了我的答案,但我不会再进一步了,因为在这一点上,它刚刚开始成为一个“当我来自excel-101时如何进行POO”;-)@Joelad,如果您只使用
value
Count
属性,然后将
列表值替换为
int Count
并调用
新的门类型(standard.Count,nameof(standard))
我在
var arrayL1=string.Join(“,”之前得到了查询的答案,,门类型。选择(p=>p.Name));添加下拉列表(“B2”,数组1)