Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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_String_Concatenation - Fatal编程技术网

c#连接列表<;字符串>;加号字符串

c#连接列表<;字符串>;加号字符串,c#,arrays,string,concatenation,C#,Arrays,String,Concatenation,我用c#写了这个清单: 所以,我想这样做 var joinstring = ""; joinstring = cad_analise[0] + ", second is " + cad_analise[1] + ", join the words."; 但是,我得到了以下错误: Index was out of range. Must be non-negative and less than the size of the collection. 我在这里做了一些测试,我认为错误在于我

我用c#写了这个清单:

所以,我想这样做

var joinstring = ""; 

joinstring = cad_analise[0] + ", second is " + cad_analise[1] + ", join the words.";
但是,我得到了以下错误:

Index was out of range. Must be non-negative and less than the size of the collection.
我在这里做了一些测试,我认为错误在于我加入了cad_analise[0]和cad_analise[1]

请参见此处的错误:

这里工作得很好:

您可以验证,您的(如问题中所述)代码工作正常

我发现的唯一问题是最后一行末尾缺少分号(但我猜这是复制/粘贴错误):

在代码示例中,您将看到以下布尔值:

    bool has_analiseCR = true;
    bool has_analiseFQ = true;
    bool has_analisePCB = false;
    bool has_analise2FAL = false;
    bool has_analiseGP = false;
然后检查是否为真,将值添加到
has\u analise List
如果为假,将其添加到
cad\u analise List
。因此,最后:

has_analise将看起来像:

[0]=>“CR”

[1] =>“FQ”

然后您在第74行尝试:

print_analise_cad = "Análise " + has_analise[0] + ", " + has_analise[1] + " e " + has_analise[2] + " do equipamento " + NumSerie_app + " e amostra de " + data_amostra + " foram cadastradas.";
您正在调用的
具有[2]
此索引不会如上所示退出。 列表仅在索引[0]和[1]上有值,而在[2]上没有值

此外,您正在检查
number\u analise\u cad
如果(number\u analise\u cad==3)
,而不是检查现有
number\u analise\u的长度
是否基于
已分析列表的长度


这就是索引超出范围异常的原因。

您在问题中使用了3个不同的变量名:
cad\u analise
has\u analise
analise
什么是
analise
我看不出您是否初始化了它。你是说用
cad\u analise
代替你的代码工作吗。没有什么错:看看你为什么不简单地使用调试器来逐步调试你的代码,看看会发生什么?@kfm如果你把所有的if条件都改成使用
number\u analise\u existente
,那么它就会起作用,因为你的代码依赖于
has\u analise
中的项目,没有兄弟,谢谢你,看看我放的链接,我想现在更清楚了。在这里看到错误:这里工作得很好:,它在if中(number\u analise\u cad==3)。我已经在评论中告诉OP:
不工作,因为你检查number\u analise\u cad是否为3,但然后你尝试在has\u analise的索引2处获取项目,has\u analise中只有2个项目而不是3
伙计们谢谢!我的代码中真正的问题是我混淆了我的变量,用你的答案我看到了错误,谢谢大家。
joinstring = cad_analise[0] + ", second is " + cad_analise[1] + ", join the words.";
    bool has_analiseCR = true;
    bool has_analiseFQ = true;
    bool has_analisePCB = false;
    bool has_analise2FAL = false;
    bool has_analiseGP = false;
print_analise_cad = "Análise " + has_analise[0] + ", " + has_analise[1] + " e " + has_analise[2] + " do equipamento " + NumSerie_app + " e amostra de " + data_amostra + " foram cadastradas.";