Re: How can I pass a multidimensional array as a ref parameter in func

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



vmsgman <vmsgman@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
> Here is a code sample ...
>
> int blah = ReadFile( defArray[,], defFileName, w, h);
>
> // Read File Contents into memory array and return for processing
> public int ReadFile( ref ushort[,] nArray, string sFname, int w, int h)
> {
> FileStream fs = new FileStream(sFname, FileMode.Open, FileAccess.Read);
> BinaryReader br = new BinaryReader(fs);
> // Read data
> for (int y=0; y<h; y++)
> {
> for (int x=0; x<w; x++)
> {
> nArray[x,y] = br.ReadUInt16();
> }
> }
> br.Close();
> fs.Close();
> return 0;
> }
>
> Can anyone help get this to work and compile ???

You pass arrays by reference in exactly the same way as you pass
anything else by reference:

int blah = ReadFile (ref defArray, defFileName, w, h);

However, it seems to me that it would be more appropriate as a return
value in the above - it's the result of reading the file, and currently
you're just returning 0. If that's meant to be a status code, you
should consider using exceptions for indicating errors.

You should also use "using" blocks (or their equivalent, try/finally
blocks) to make sure that files get closed whether or not an exception
is thrown.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.



Relevant Pages