开发者

Cannot implicitly convert type 'int' to 'int[]'

开发者 https://www.devze.com 2023-04-10 09:51 出处:网络
I have declared my int[] as follows int[] iroleID = new int[] { }; My code for getting the values from database and assignin开发者_StackOverflow社区g to iroleid is as follows

I have declared my int[] as follows

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

My code for getting the values from database and assignin开发者_StackOverflow社区g to iroleid is as follows

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


And no surprise here. Look at

iroleID = Convert.ToInt32(...);

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

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 exactly to do as you don't show what the purpose of iRoleID is.


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(...)) )


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:

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


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.

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.


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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消