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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



AHN <anerse@xxxxxxxxxx> wrote:
> That compiles and works. Just try.

I have. Have you? Try compiling the following, and watch it fail.

using System;

public class Test
{
static int ReadFile(ref ushort[,] nArray,
string sFname, int w, int h)
{
// Implementation skipped
return 0;
}

static void Main()
{
ushort[,] defArray = null;
string defFileName = null;
int w = 0;
int h = 0;
int blah = ReadFile( defArray, defFileName, w, h);
}
}

I get the following with 2.0 beta 2:

Test.cs(18,20): error CS1502: The best overloaded method match for
'Test.ReadFile(ref ushort[*,*], string, int, int)' has some invalid
arguments
Test.cs(18,30): error CS1620: Argument '1' must be passed with the
'ref' keyword

And this with 1.1:
Test.cs(17,20): error CS1502: The best overloaded method match for
'Test.ReadFile(ref ushort[*,*], string, int, int)' has some
invalid arguments
Test.cs(17,30): error CS1503: Argument '1': cannot convert from 'ushort
[*,*]' to 'ref ushort[*,*]'

Both seem reasonably clear to me.

> Array variable is the pointer to the address. So when you give it as
> argument, anithing done to it in the function body is done to the
> array itself.

Changes to the value of the parameter itself, however (rather than the
array it references) will not be visible to the caller unless the
parameter is passed by reference.

> No 'ref' is needed. Ref is needed if your variable is not a reference
> type. Array is a reference type. Cheers.

I'm sorry, but you clearly don't understand what "ref" actually means.

Once again, please read
http://www.pobox.com/~skeet/csharp/parameters.html

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



Relevant Pages

  • Re: slicing array references
    ... I'm working on some classes with attributes that are array and hash ... 0,1,2 in scalar context is 2. ... there is no need to try variations on how the ref got there. ... similarly, when you just want a single item from an aggregate reference, always use the -> notation. ...
    (perl.beginners)
  • Re: textbook authors: passing by ref is NOT more efficient!
    ... efficiency in terms of performance and we are talking reference objects. ... classic example (using int values, ... Console.WriteLine("Value after by ref routine ", ... reference to the array object even if you don't say "ref", ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: textbook authors: passing by ref is NOT more efficient!
    ... Ask them how long it takes to write a screen compared to the time difference in using 'ref' or not. ... reference to the array object even if you don't say "ref", ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: how to pass array and varaible
    ... ref even if you use a direct array. ... Make sure you pass var first, followed by array. ... > they no longer complicate argument passing. ... > a reference can take significantly less time. ...
    (perl.beginners)
  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)