Re: How can I pass a multidimensional array as a ref parameter in func
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sun, 24 Jul 2005 08:19:41 +0100
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
.
- Follow-Ups:
- References:
- How can I pass a multidimensional array as a ref parameter in func
- From: vmsgman
- Re: How can I pass a multidimensional array as a ref parameter in func
- From: AHN
- Re: How can I pass a multidimensional array as a ref parameter in func
- From: Jon Skeet [C# MVP]
- Re: How can I pass a multidimensional array as a ref parameter in func
- From: AHN
- How can I pass a multidimensional array as a ref parameter in func
- Prev by Date: Read file from zip
- Next by Date: Re: Experience Kit
- Previous by thread: Re: How can I pass a multidimensional array as a ref parameter in func
- Next by thread: Re: How can I pass a multidimensional array as a ref parameter in func
- Index(es):
Relevant Pages
|