Swanand
Swanand

Reputation: 4115

Passing one Dimension of a Two Dimensional Array in C#

I have moved from C to C#. I have a function which accepts an array. I want to pass one dimension of a Two Dimensional array to this function.

C Code would be:-

void array_processing(int * param); 

void main()
{
  int Client_ID[3][50];
  /* Some 
     Processing 
     which fills 
     this array */
    array_processing(&Client_ID[1]);
}

Now, When I want to do same in C#, How can I pass this array? Function defination will look like:-

private void array_processing(ref int[] param);

and Array would be declared as :-

int[,] Client_ID = new int[3,50];

Now How can I pass Client_ID[1] to the function array_processing()??

By doing array_processing ( ref Client_ID[1]) shouts as "Wrong Number of Indices"!

Upvotes: 5

Views: 16944

Answers (6)

Late to the conversation, but here is a jagged array example to do this:

string[][] rows = GetStringArray(values);
string[] row = rows[0];

You would set up your jagged array something like:

// rowCount from runtime data
stringArray = new string[rowCount][];

for (int index = 0; index < rowCount; index++)
{
    // columnCount from runtime data
    stringArray[index] = new string[columnCount];

    for (int index2 = 0; index2 < columnCount; index2++)
    {
        // value from runtime data
        stringArray[index][index2] = value;
    }
}

Upvotes: 0

zmbq
zmbq

Reputation: 39013

You can't really do that. C# is less outgoing about its arrays, and prevents you from doing C-like manipulations. This is a good thing.

You have various options:

  1. Create a 1D array and copy your 2D row to it.
  2. Use a jagged array - an array of arrays, which is more like what C lets you do.
  3. Have an array_processing overload that takes a 2D array and a row number.

  4. If you really want to access a 2D row as a 1D array, you should create a 'RowProxy' class that will implement the IList interface and let you access just one row:

    class RowProxy<T>: IList<T>
    {
        public RowProxy(T[,] source, int row)
        { 
           _source = source;
           _row = row;
        }
    
        public T this[int col]
        {
            get { return _source[_row, col]; } 
            set { _source[_row, col] = value; }
        }
    
        private T[,] _source;
        private int _row;
    
        // Implement the rest of the IList interface
    }
    
  5. Use a lambda expression that will lose the array semantics, but is rather cool:

    var ClientId = ...;
    
    var row_5_accessor = (c=>ClientId[5, c]);
    

    You can use row_5_accessor as a function, row_5_accessor(3) will give you ClientId[5, 3]

Upvotes: 8

Vladimir P
Vladimir P

Reputation: 199

You could declare you array as

int[][] Client_ID = new[] { new int[50], new int[50], new int[50] };

and then you can pass it to your array_processing function

array_processing(ref Clinet_ID[1]);

Sorry for miss of my pen.

Upvotes: 0

Tung
Tung

Reputation: 5434

You can use a jagged array

// Initialize jagged array
int[][] clientID = new int[3][];
for (int i=0; i<clientId.Length; i++)
{
   clientId[i] = new int[50];
}

array_processing(ref clientId[1]);

And your method:

private void array_processing(ref int[] subArray);

Upvotes: 1

Maxim V. Pavlov
Maxim V. Pavlov

Reputation: 10509

A primitive way would be:

var dimNumber = 1;

int[] oneDimension = new int[50];

for(var i=0; i<50; i++)
{
   oneDimension[i] = Client_ID[dimNumber][i];
}

array_processing ( ref oneDimension);

I would suggest using Lambda expressions like in the way 5 of zmbq's answer.

Upvotes: 0

Aleksej Vasinov
Aleksej Vasinov

Reputation: 2797

Just declare method

private void ParseArray(int[,] ar)
{
    // Some work...
}
  • UDP: Code format

Upvotes: 0

Related Questions