为什么编译器将此变量标记为未分配? [英] Why is the compiler flagging this variable as unassigned?

查看:78
本文介绍了为什么编译器将此变量标记为未分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用未分配的局部变量''fs''


请参阅我在哪里指出编译器在哪里标记此错误

方法如下。 fs在try块的第一行初始化,所以

为什么在finally块中标记为未分配?


感谢您的帮助!


- Joe Geretz -


private byte [] ReadFile(string FileSpec)

{

FileStream fs;

尝试

{

fs = File.Open(FileSpec,FileMode.Open,FileAccess.Read);

int FileLen =(int)fs.Length;

byte [] FileBuffer = new byte [FileLen -1];

fs.Read( FileBuffer,0,FileLen);

返回FileBuffer;

}

catch(例外e)

{

抛出新的异常(无法打开+ FileSpec,e);

}

终于

{

fs.Close(); < - 此行标记错误

}

}

Use of unassigned local variable ''fs''

Please see where I''ve indicated where the compiler is flagging this error in
the method below. fs is initialized in the first line in the try block, so
why is it flagged as unassigned in the finally block?

Thanks for your help!

- Joe Geretz -

private byte[] ReadFile(string FileSpec)
{
FileStream fs;
try
{
fs = File.Open(FileSpec, FileMode.Open, FileAccess.Read);
int FileLen = (int) fs.Length;
byte[] FileBuffer = new byte[FileLen -1];
fs.Read(FileBuffer, 0, FileLen);
return FileBuffer;
}
catch (Exception e)
{
throw new Exception("Could not open " + FileSpec, e);
}
finally
{
fs.Close(); <-- Error flagged on this line
}
}

推荐答案

Joseph,


原因是编译器无法确定fs已被分配的事实

a值。例如,如果在调用

File.Open()期间发生异常,则fs不会分配值。使用:


FileStream fs = null;


编译器将停止投诉。


问候 - Octavio

" Joseph Geretz" < JG ***** @ nospam.com> escribióenel mensaje

news:ev ************** @ TK2MSFTNGP09.phx.gbl ...
Joseph,

The reason is compiler can not be sure of the fact that fs has been assigned
a value at that point. For instance, if exception happens during the call to
File.Open(), fs doesn''t get a value assigned. Use:

FileStream fs = null;

and the compiler will stop complain.

Regards - Octavio
"Joseph Geretz" <jg*****@nospam.com> escribió en el mensaje
news:ev**************@TK2MSFTNGP09.phx.gbl...
使用未分配的本地变量''fs''

请参阅下面的方法,看看我在哪里指出了编译器标记此错误的位置。 fs在try块的第一行初始化,
为什么在finally块中标记为未分配?

感谢您的帮助!

- Joe Geretz -

私有字节[] ReadFile(string FileSpec)
{FileStream fs;
尝试
{
fs =文件。打开(FileSpec,FileMode.Open,FileAccess.Read);
int FileLen =(int)fs.Length;
byte [] FileBuffer = new byte [FileLen -1];
fs。读取(FileBuffer,0,FileLen);
返回FileBuffer;
}
catch(例外e)
{
抛出新的异常(无法打开) + FileSpec,e);
}
终于
{
fs.Close(); < - 此行标记错误
}
}
Use of unassigned local variable ''fs''

Please see where I''ve indicated where the compiler is flagging this error
in the method below. fs is initialized in the first line in the try block,
so why is it flagged as unassigned in the finally block?

Thanks for your help!

- Joe Geretz -

private byte[] ReadFile(string FileSpec)
{
FileStream fs;
try
{
fs = File.Open(FileSpec, FileMode.Open, FileAccess.Read);
int FileLen = (int) fs.Length;
byte[] FileBuffer = new byte[FileLen -1];
fs.Read(FileBuffer, 0, FileLen);
return FileBuffer;
}
catch (Exception e)
{
throw new Exception("Could not open " + FileSpec, e);
}
finally
{
fs.Close(); <-- Error flagged on this line
}
}



Joseph,


原因是编译器不能确定fs已被分配的事实

a值。例如,如果在调用

File.Open()期间发生异常,则fs不会分配值。使用:


FileStream fs = null;


编译器将停止投诉。


问候 - Octavio

" Joseph Geretz" < JG ***** @ nospam.com> escribióenel mensaje

news:ev ************** @ TK2MSFTNGP09.phx.gbl ...
Joseph,

The reason is compiler can not be sure of the fact that fs has been assigned
a value at that point. For instance, if exception happens during the call to
File.Open(), fs doesn''t get a value assigned. Use:

FileStream fs = null;

and the compiler will stop complain.

Regards - Octavio
"Joseph Geretz" <jg*****@nospam.com> escribió en el mensaje
news:ev**************@TK2MSFTNGP09.phx.gbl...
使用未分配的本地变量''fs''

请参阅下面的方法,看看我在哪里指出了编译器标记此错误的位置。 fs在try块的第一行初始化,
为什么在finally块中标记为未分配?

感谢您的帮助!

- Joe Geretz -

私有字节[] ReadFile(string FileSpec)
{FileStream fs;
尝试
{
fs =文件。打开(FileSpec,FileMode.Open,FileAccess.Read);
int FileLen =(int)fs.Length;
byte [] FileBuffer = new byte [FileLen -1];
fs。读取(FileBuffer,0,FileLen);
返回FileBuffer;
}
catch(例外e)
{
抛出新的异常(无法打开) + FileSpec,e);
}
终于
{
fs.Close(); < - 此行标记错误
}
}
Use of unassigned local variable ''fs''

Please see where I''ve indicated where the compiler is flagging this error
in the method below. fs is initialized in the first line in the try block,
so why is it flagged as unassigned in the finally block?

Thanks for your help!

- Joe Geretz -

private byte[] ReadFile(string FileSpec)
{
FileStream fs;
try
{
fs = File.Open(FileSpec, FileMode.Open, FileAccess.Read);
int FileLen = (int) fs.Length;
byte[] FileBuffer = new byte[FileLen -1];
fs.Read(FileBuffer, 0, FileLen);
return FileBuffer;
}
catch (Exception e)
{
throw new Exception("Could not open " + FileSpec, e);
}
finally
{
fs.Close(); <-- Error flagged on this line
}
}



try块的第一行可能会立即抛出异常

(例如拒绝访问),在这种情况下,你的fs变量确实会在finally中取消分配。块。


Bennie Haelen


Joseph Geretz写道:
The first line of your try block could immediately throw an exception
(e.g. "Access Denied"), in which case your fs variable would indeed be
unassigned in the "finally" block.

Bennie Haelen

Joseph Geretz wrote:
使用未分配的局部变量''fs''

请参阅下面的方法,看看我在哪里指出了编译器在哪里标记了这个错误。 fs在try块的第一行初始化,所以
为什么在finally块中标记为未分配?

感谢您的帮助!

- Joe Geretz -

私有字节[] ReadFile(string FileSpec)
{FileStream fs;
尝试
{
fs =文件。打开(FileSpec,FileMode.Open,FileAccess.Read);
int FileLen =(int)fs.Length;
byte [] FileBuffer = new byte [FileLen -1];
fs。读取(FileBuffer,0,FileLen);
返回FileBuffer;
}
catch(例外e)
{
抛出新的异常(无法打开) + FileSpec,e);
}
终于
{
fs.Close(); < - 此行标记错误
}
}
Use of unassigned local variable ''fs''

Please see where I''ve indicated where the compiler is flagging this error in
the method below. fs is initialized in the first line in the try block, so
why is it flagged as unassigned in the finally block?

Thanks for your help!

- Joe Geretz -

private byte[] ReadFile(string FileSpec)
{
FileStream fs;
try
{
fs = File.Open(FileSpec, FileMode.Open, FileAccess.Read);
int FileLen = (int) fs.Length;
byte[] FileBuffer = new byte[FileLen -1];
fs.Read(FileBuffer, 0, FileLen);
return FileBuffer;
}
catch (Exception e)
{
throw new Exception("Could not open " + FileSpec, e);
}
finally
{
fs.Close(); <-- Error flagged on this line
}
}



这篇关于为什么编译器将此变量标记为未分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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