运算符'+'不能应用于'string []'和'int'类型的操作数 [英] Operator '+' cannot be applied to operands of type 'string[]' and 'int'

查看:184
本文介绍了运算符'+'不能应用于'string []'和'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 is b 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 using x since a 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 into a 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 use dt 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆