Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello every one
is it possible to pass a 3-dimension array to a method that has a 2-dimension array as an argument by making one dimension constant. if possible, how should the passing argument (3-D array) looks like?

for example 3-D array is Array_Name[i,j,k] and i want to pass Array_Name[i,j,2] to the method.
Posted
Updated 5-Mar-12 6:40am
v2

Your example is not passing the array, this is passing the array element, which is the same as passing a variable of the same type as the array element.

However, you can path an array as well, with no hassles. The arrays are reference types, so you simply need to pass the array reference itself by value.

C#
double[,,] doubleArray = new double[4,8,12]; // 3D array of a primitive type initialized as 4x8x12 array

struct ReallyBigStructure {/*...*/} // just to demonstrate you can work with them in arrays too; value type
class SomeElement {/*...*/} // reference type

ReallyBigStructurep[,,] arrayOfStructures = new ReallyBigStructurep[10,20,3];
SomeElement[,,] arrayOfClassInstances = new SomeElement[11,21,31];

//...

// pass all the arrays by value, as they are reference types:
void WorkWithDoubleArray(double[,,] array) {/* ... */} 
void WorkWithStructureArray(ReallyBigStructure[,,] array) {/* ... */}
void WorkWittClassInstanceArray(SomeElement[,,] array) {/* ... */}

// and the calls would be:
void WorkWithDoubleArray(doubleArray);
void WorkWithStructureArray(arrayOfStructures);
void WorkWittClassInstanceArray(arrayOfClassInstances);

// if you need to pass array element, it's no different from passing of a parameter of element type;
void PassAClassInstance(SomeElement value); //never need ref or out
void PassDoubleByValue(double value);
void PassDoubleReference(ref double value); //can input and return value, but it makes no sense as you use return
void OutpuDoubleByReference(out double value); //output-only value, but it makes no sense as you use return param

// but it would make sense as otherwise you would copy it all on stack:
void PassBigStructureByReference(ref ReallyBigStructure);
// out makes sense, too, but by-value passing may have a problem of too copying too much data on stack;
// not a problem for smaller structured


—SA
 
Share this answer
 
if, as SAKryukov states, you simply want to define a constantant 3rd indexer then your method should just take 3 ints for indexer values and you can pass your sonstant to the third,

However, on the offchance that you are actually talking about jagged arrays (an array of arrays of arrays of values, for a 3D jagged array) you could try an extension method on array, something along the lines of

C#
public static T[][][] AddConstantDimension<t>(this T[][] array, int extraDimensionLength, T extraVal)
        {
            T[][][] result = new T[][][] {};
            
            for (int i = 0; i < array.GetLength(0); i++)
            {
                T[][] inner = new T[][] { };
                
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    T[] inner2 = new[] {extraVal};
                    inner[j] = inner2;
                }

                result[i] = inner;
            }

            return result;
        }

</t>


Then your call to your method would be

C#
someObject.Method(2dJaggedArray.AddConstantDimension(5,2))


which would add a 3rd dimension, of length 5, with the value 2 inserted

NB: none of the above is tested, but should provide at least a starting point
 
Share this answer
 
Try Tuples in .NET Framework. Ref at: http://msdn.microsoft.com/en-us/library/system.tuple.aspx[^]
 
Share this answer
 

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