views:

58

answers:

4
function(int[] me)
{
    //Whatever
}

main()
{
    int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };    
    function(numbers[1]);
}

So here, I'd want to pass int[] {3,4} to the function... but that doesn't work. Is there a way to do that?

A: 

int[] {3,4} refers to a location, not an array and holds an int. Your function should be

function(int me)
{
    //Whatever
}

this is how you will get value and pass to your function

    int valueFromLocation = numbers[3,4];

    function(valueFromLocation )
    {
        //Whatever
    }

EDIT:

if you need whole array in any case use Jagged Arrays

int[][] jaggedArray =
     new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

int[] array1 =  jaggedArray[1];
int[] array1 =  jaggedArray[2];

now you can pass it the way you want

function(int[] array){}
Asad Butt
he wants to pass a int[], that's the question.
John Boker
Thanks, " int[] {3,4} " was confusing. have fixed my answer now
Asad Butt
+2  A: 

I think you want to use a jagged array instead of a 2d array. There is more information here:

http://msdn.microsoft.com/en-us/library/aa288453%28VS.71%29.aspx

an example from that page is:

int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

John Boker
A: 

No you can't do that, but you can convert it by yourself. Something like.

 int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {8, 6} };

           List<int[]> arrays = new List<int[]>();
           for (int i = 0; i < 3; i++)
           {
               int[] arr = new int[2];
               for (int k = 0; k < 2; k++)
               {
                   arr[k] = numbers[i, k];
               }

               arrays.Add(arr);
           }

           int[] newArray = arrays[1]; //will give int[] { 3, 4}
Stan R.
well i guess i took the question too literally then..eh?
Stan R.
A: 
function(int[] me) 
{ 
    //Whatever 
} 

main() 
{ 
    int[][] numbers = new int[3][] { new int[2] {1, 2}, new int[2]{3, 4}, new int[2] {5, 6} };     
    function(numbers[1]); 
} 

This is called a jagged array (an array of arrays). If you can't change the definition of numbers, you would need to write a function which can

Martin Booth