无法将类型'int'隐式转换为'int []'

时间:2022-05-01 16:04:54

I have declared my int[] as follows

我已经声明我的int []如下

int[] iroleID = new int[] { };

int [] iroleID = new int [] {};

My code for getting the values from database and assigning to iroleid is as follows

我从数据库获取值并分配给iroleid的代码如下

if (oAuthenticate.getTaskID(out m_oDataSet1, "uspgetTaskID"))
{
  for (int iTaskid = 0; iTaskid < m_oDataSet1.Tables[0].Rows.Count; iTaskid++)
  {
   iroleID = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());
   strTaskID = m_oDataSet1.Tables[0].Rows[iTaskid]["TaskID"].ToString();
   arrTaskID.Add(strTaskID);
   }
}

But i am getting an error as mentioned Cannot implicitly convert type 'int' to 'int[]' can any one help me

但我收到错误提到不能隐式转换类型'int'到'int []'可以任何人帮助我

6 个解决方案

#1


4  

and no suprise here. Look at

这里并不令人惊讶。看着

iroleID = Convert.ToInt32(...);

Convert.ToIn32 results in an int just like the compiler claims. Either do something like

Convert.ToIn32就像编译器声称的那样产生一个int。要么像这样做

if (oAuthenticate.getTaskID(out m_oDataSet1, "uspgetTaskID"))
{
  var iroleID = new int[m_oDataSet1.Tables[0].Rows.Count];
  for (int iTaskid = 0; iTaskid < m_oDataSet1.Tables[0].Rows.Count; iTaskid++)
  {
   iroleID[iTaskid] = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());
    /* ... */
   }
}

or rethink your algorithm.

或重新考虑您的算法。

PS: I can hardly tell you what excactly to do as you don't show what the purpose of iRoleID is

PS:我几乎无法告诉你要做什么,因为你没有表明iRoleID的目的是什么

#2


1  

off course!

 iroleID = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());

iroleID is an int array; Convert.ToInt32() returns an int .
so:
-you must declare an int variable tu store Convert.ToInt32() value
or
-just add Convert.ToInt32() result to iroleID ( iroleID.Add(Convert.ToInt32(...)) )

iroleID是一个i​​nt数组; Convert.ToInt32()返回一个int。所以: - 你必须声明一个int变量tu存储Convert.ToInt32()值或-just将Convert.ToInt32()结果添加到iroleID(iroleID.Add(Convert.ToInt32(...)))

#3


0  

Sure, you can't just assign the int value to the int[] variable. Probably you need to add items to the collection. Change your int[] iroleID = new int[] { }; to List<int> iroleID = new List<int>(); and then change code to:

当然,你不能只将int值赋给int []变量。您可能需要在集合中添加项目。改变你的int [] iroleID = new int [] {}; to List iroleID = new List ();然后将代码更改为:

if (oAuthenticate.getTaskID(out m_oDataSet1, "uspgetTaskID"))
{
  for (int iTaskid = 0; iTaskid < m_oDataSet1.Tables[0].Rows.Count; iTaskid++)
  {
   iroleID.Add(Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString()));
   strTaskID = m_oDataSet1.Tables[0].Rows[iTaskid]["TaskID"].ToString();
   arrTaskID.Add(strTaskID);
   }
}

#4


0  

iroleID = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());

This would convert the RoleID columns value to an integer. You are taking this integer and trying to assign it to an integer array which of course is not possible. What exactly is your idea? If it is to populate a single array with all the IDs you can do the same as you did but assign it to a normal integer and then add that integer to a list or something.

这会将RoleID列值转换为整数。您正在获取此整数并尝试将其分配给整数数组,这当然是不可能的。你的想法究竟是什么?如果要使用所有ID填充单个数组,则可以执行相同操作,但将其分配给普通整数,然后将该整数添加到列表或其他内容中。

You can also just use plain ADO.NET to retrieve all the values from that column and cast it to an List. That would be better.

您也可以使用普通的ADO.NET来检索该列中的所有值并将其强制转换为List。那会更好。

#5


0  

One problem has already been answered, you must add it to an array giving the position you want

已经回答了一个问题,您必须将其添加到给出您想要的位置的数组中

iroleID[iTaskid] = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());

the other problem is, that you create an array. You must give the length of the array.

另一个问题是,你创建了一个数组。您必须给出数组的长度。

int[] iroleID = new int[m_oDataSet1.Tables[0].Rows.Count];

int [] iroleID = new int [m_oDataSet1.Tables [0] .Rows.Count];

#6


0  

Use indexing for the int array to assign a value, you can't assign an integer directly to an integer array.

使用索引为int数组赋值,不能将整数直接赋给整数数组。

iroleID[0] = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());

#1


4  

and no suprise here. Look at

这里并不令人惊讶。看着

iroleID = Convert.ToInt32(...);

Convert.ToIn32 results in an int just like the compiler claims. Either do something like

Convert.ToIn32就像编译器声称的那样产生一个int。要么像这样做

if (oAuthenticate.getTaskID(out m_oDataSet1, "uspgetTaskID"))
{
  var iroleID = new int[m_oDataSet1.Tables[0].Rows.Count];
  for (int iTaskid = 0; iTaskid < m_oDataSet1.Tables[0].Rows.Count; iTaskid++)
  {
   iroleID[iTaskid] = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());
    /* ... */
   }
}

or rethink your algorithm.

或重新考虑您的算法。

PS: I can hardly tell you what excactly to do as you don't show what the purpose of iRoleID is

PS:我几乎无法告诉你要做什么,因为你没有表明iRoleID的目的是什么

#2


1  

off course!

 iroleID = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());

iroleID is an int array; Convert.ToInt32() returns an int .
so:
-you must declare an int variable tu store Convert.ToInt32() value
or
-just add Convert.ToInt32() result to iroleID ( iroleID.Add(Convert.ToInt32(...)) )

iroleID是一个i​​nt数组; Convert.ToInt32()返回一个int。所以: - 你必须声明一个int变量tu存储Convert.ToInt32()值或-just将Convert.ToInt32()结果添加到iroleID(iroleID.Add(Convert.ToInt32(...)))

#3


0  

Sure, you can't just assign the int value to the int[] variable. Probably you need to add items to the collection. Change your int[] iroleID = new int[] { }; to List<int> iroleID = new List<int>(); and then change code to:

当然,你不能只将int值赋给int []变量。您可能需要在集合中添加项目。改变你的int [] iroleID = new int [] {}; to List iroleID = new List ();然后将代码更改为:

if (oAuthenticate.getTaskID(out m_oDataSet1, "uspgetTaskID"))
{
  for (int iTaskid = 0; iTaskid < m_oDataSet1.Tables[0].Rows.Count; iTaskid++)
  {
   iroleID.Add(Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString()));
   strTaskID = m_oDataSet1.Tables[0].Rows[iTaskid]["TaskID"].ToString();
   arrTaskID.Add(strTaskID);
   }
}

#4


0  

iroleID = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());

This would convert the RoleID columns value to an integer. You are taking this integer and trying to assign it to an integer array which of course is not possible. What exactly is your idea? If it is to populate a single array with all the IDs you can do the same as you did but assign it to a normal integer and then add that integer to a list or something.

这会将RoleID列值转换为整数。您正在获取此整数并尝试将其分配给整数数组,这当然是不可能的。你的想法究竟是什么?如果要使用所有ID填充单个数组,则可以执行相同操作,但将其分配给普通整数,然后将该整数添加到列表或其他内容中。

You can also just use plain ADO.NET to retrieve all the values from that column and cast it to an List. That would be better.

您也可以使用普通的ADO.NET来检索该列中的所有值并将其强制转换为List。那会更好。

#5


0  

One problem has already been answered, you must add it to an array giving the position you want

已经回答了一个问题,您必须将其添加到给出您想要的位置的数组中

iroleID[iTaskid] = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());

the other problem is, that you create an array. You must give the length of the array.

另一个问题是,你创建了一个数组。您必须给出数组的长度。

int[] iroleID = new int[m_oDataSet1.Tables[0].Rows.Count];

int [] iroleID = new int [m_oDataSet1.Tables [0] .Rows.Count];

#6


0  

Use indexing for the int array to assign a value, you can't assign an integer directly to an integer array.

使用索引为int数组赋值,不能将整数直接赋给整数数组。

iroleID[0] = Convert.ToInt32(m_oDataSet1.Tables[0].Rows[iTaskid]["RoleID"].ToString());