如何在func中将多维数组作为ref参数传递 [英] How can I pass a multidimensional array as a ref parameter in func

查看:107
本文介绍了如何在func中将多维数组作为ref参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个代码示例...


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


//将文件内容读入内存数组并返回进行处理

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);

//读取数据

for(int y = 0; y< h; y ++)

{

for(int x = 0; x< w; x ++)

{

nArray [x,y] = br.ReadUInt16();

}

}

br.Close();

fs.Close();

返回0;

}


任何人都可以帮忙让这个工作并编译???


THX greg

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 ???

THX greg

推荐答案

vmsgman< vm ***** @ discussion.microsoft.com>写道:
vmsgman <vm*****@discussions.microsoft.com> wrote:
这是一个代码示例...

int blah = ReadFile(defArray [,],defFileName,w,h);
//将文件内容读入内存数组并返回进行处理
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);
//读取数据
(int y = 0; y< h; y ++ )
{
for(int x = 0; x< w; x ++)
{
nArray [x,y] = br.ReadUInt16();
}
}
br.Close();
fs.Close();
返回0;
}

任何人都可以帮忙得到这个工作和编译???
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 ???




您通过引用传递数组的方式与通过引用传递

其他任何方式完全相同:


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


然而,在我看来它更合适作为一个返回
上面的
值 - 这是读取文件的结果,目前

你只是返回0。如果这意味着作为状态代码,您需要考虑使用异常来指示错误。


您还应该使用使用块(或它们的等价物,尝试/终于

块)以确保文件被关闭,无论是否抛出异常




-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


vmsgman< vm ***** @ discussion.microsoft.com>写道:
vmsgman <vm*****@discussions.microsoft.com> wrote:
这是一个代码示例...

int blah = ReadFile(defArray [,],defFileName,w,h);
//将文件内容读入内存数组并返回进行处理
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);
//读取数据
(int y = 0; y< h; y ++ )
{
for(int x = 0; x< w; x ++)
{
nArray [x,y] = br.ReadUInt16();
}
}
br.Close();
fs.Close();
返回0;
}

任何人都可以帮忙得到这个工作和编译???
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 ???




您通过引用传递数组的方式与通过引用传递

其他任何方式完全相同:


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


然而,在我看来它更合适作为一个返回
上面的
值 - 这是读取文件的结果,目前

你只是返回0。如果这意味着作为状态代码,您需要考虑使用异常来指示错误。


您还应该使用使用块(或它们的等价物,尝试/终于

块)以确保文件被关闭,无论是否抛出异常




-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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


作为旁注,为什么要使defArray

输入参数ref。有没有理由你

不要让ReadFile返回字节[]?


然后,在byte []上检查null是否成功。

-

2004和2005 Microsoft MVP C#

Robbe Morris
http://www.masterado.net


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

As a side note, why would you make defArray
an input parameter by "ref". Is there a reason you
don''t make ReadFile return the byte[]?

Then, check for null on the byte[] for success.
--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.masterado.net

Earn


这篇关于如何在func中将多维数组作为ref参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆