Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int [] array;
int [,] population;
for (i=0;i<columns,i++)
{
   for(j=0;j<rows;j++)>
   {
      population[i, j] = array;
   }
}

the error is unable to convert type int [] to int
what is wrong
Posted
Updated 2-May-13 22:32pm
v3
Comments
CHill60 3-May-13 4:33am    

Quote:
the error is unable to convert type int [] to int
what is wrong
Your approach is wrong: an array item (like population[i,j]) is a scalar type (i.e. a single number in your case) while a whole array (like array) is a collection of scalar types (that is array contains many numbers). You cannot assign a collection to a single number.

The following line
C#
population[i, j] = array[j];


would be syntactically correct (however it could be semantically wrong, it could produce a runtime error, etc...).


[Update]
You cannot do that with bidimensional arrays, you have to use jagged arrays, see, for instance:
"c# assign 1 dimensional array to 2 dimensional array syntax"[^] at Stack Overflow.
[/Update]
 
Share this answer
 
v2
Comments
mahmoud.abdallah 3-May-13 4:41am    
how tom make 2dArray Acccept an array in each index of it
CPallini 3-May-13 6:17am    
See my update.
mahmoud.abdallah 3-May-13 8:27am    
thanks you helped me a lot >>>in Arabic langusge shokran gedan
CPallini 3-May-13 8:32am    
You are welcome. In Italian: "Prego".
your code is wrong..
if u want to use an element inside an array You must specify the index like:
C#
for(int i = 0;i<10;i++)
{
  for(int j = 0; j< 20; j++)
  {
    Population[i][j] = array[i];
  }
}


it means : hey compiler take the located at array and index i an put that in population at index i and j.
 
Share this answer
 
v2
Comments
mahmoud.abdallah 3-May-13 4:42am    
i do not want one value i want to put the whole array in each index of the 2dArray
its compiler error but obeous..
your assing int to int[].
code should be like,
C++
int array;
int Population[10][20];
for(int i = 0;i<10;i++)
{
  for(int j = 0; j< 20; j++)
  {
    Population[i][j] = array;
  }
}
 
Share this answer
 
Comments
CHill60 3-May-13 4:39am    
This doesn't even compile!
Coder Block 3-May-13 4:42am    
Variable name array is predefine used in system.change it to array1.
mahmoud.abdallah 3-May-13 4:44am    
i wrote it like that just to express my problem >>>thanks for your comment
CHill60 3-May-13 4:52am    
I spotted that bit - how about the error "Syntax error, bad array declarator."?
mahmoud.abdallah 3-May-13 4:43am    
that is what i did

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900