运算符'+'不能应用于'string []'和'int'类型的操作数 [英] Operator '+' cannot be applied to operands of type 'string[]' and 'int'
问题描述
protected void ddlCircle_SelectedIndexChanged(对象发件人,EventArgs e)
{
ArrayList list = new ArrayList();
ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter cd;
cd = new ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter();
DataTable dt = new DataTable();
dt = cd.GetAvailableData(ddlCircle.SelectedValue);
int x;
x = dt.Rows.Count;
String [] a;
字符串 [] b = { 团队};
for ( int i = 1 ; i < = x; i ++)
{
a = b + i; // 此处出现错误
}
}
尝试:
这是一些非常奇怪的代码:它不是很明显你期望从中得到什么 - 但是到目前为止还有更多的错误!
首先,a
是一个字符串数组 - 除了你没有' t分配任何空间来保存数据。
其次,为什么b
和字符串数组(如果你只在那里放一个元素),每次使用相同的元素?为什么不只是一个字符串?
第三,你期望什么代码
a = b + i;
实际上要做什么?特别是在循环中?
由于你没有在循环中改变b,整个循环可以用x
替换为单个语句无论如何,a
总是会包含相同的值。
a是一个数组(没有数据),b是一个数组(带有一个元素),我是一个整数 - 所以我看不出任何特别有用的东西进入a
无论如何。
想想你想要实现的目标,而不是你到目前为止所写的内容:因为我认为你已经忘记了这一点 - 我怀疑你打算使用dt
在那里使用你在早期代码中检索的数据!
这里的意图似乎生成了名为Teamx的x队列。请更改您的命名以更好地反映变量的意图。
执行此操作:
ArrayList teamList = new ArrayList;
for ( int i = 1 ; i < = x; i ++)
{
teamList.Add( String .Format( 团队{0},i));
}
如果你真的需要字符串数组然后使用
字符串 []团队;
teams = teamList.ToArray( typeof ( string ));
protected void ddlCircle_SelectedIndexChanged(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter cd;
cd = new ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter();
DataTable dt = new DataTable();
dt = cd.GetAvailableData(ddlCircle.SelectedValue);
int x;
x = dt.Rows.Count;
String [] a;
String [] b = { "Team" };
for (int i = 1; i <= x; i++)
{
a = b + i; // error popup here
}
}
Try:
That is some very odd code: it's not obvious exactly what you expect to get out of it - but there are more errors in there than you have seen so far!
First off,a
is an array of strings - except you haven't allocated any space to hold the data.
Second, why isb
and array of strings if you are only putting one element in there, and use the same element each time? Why not just a string?
Third, what do you expect the code
a = b + i;To actually do? Particularly in a loop?
Since you don't change b inside the loop, the whole loop can be replaced with a single statement usingx
sincea
would always contain the same value after it finishes anyway.
And a is an array (with no data), b is an array (with one element) and i is an integer - so I can't see anything particularly useful going intoa
anyway.
Think about what you are trying to achieve, rather than what you have written so far: because I think you have lost track of that - I suspect you meant to usedt
in there somewhere to use the data you retrieved in the earlier code!
The intent here seems to generate the array of x teams with names Teamx. Please change your naming to better reflect the intent of the variable.
Do this:
ArrayList teamList = new ArrayList; for (int i = 1; i <= x; i++) { teamList.Add(String.Format("Team{0}", i)); }
And if you really need string array then use
String [] teams; teams = teamList.ToArray(typeof( string ));
这篇关于运算符'+'不能应用于'string []'和'int'类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!