终于声明和对象的麻烦 [英] troubles with finally statement and objects

查看:58
本文介绍了终于声明和对象的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在我的代码中的许多地方使用try catch finally块。


当创建一个sqldatareader博士时并尝试在我的

中关闭它最后阻止我得到错误:使用无符号局部变量。


dr.close();

在try中工作正常,但在finally中没有。


如何在没有这些错误的情况下访问finally中的本地对象?


谢谢


克里斯

Hi,

I am using the try catch finally block in many places in my code.

When a create an sqldatareader dr and try to close it in my
finally block I get the error: use of unsigned local variable.

dr.close();
works fine in the try but not in the finally.

How can I access my local objects in the finally without these errors?

thanks

Chris

推荐答案

Chris,


你应该这样做:


// Delcare读者。

SqlDataReader reader = null;


//在这里使用阅读器。

尝试

{

//创建阅读器。

reader = new SqlDataReader(...);


//使用阅读器。

}

catch

{

}

终于

{

//检查读者是否为null。如果不是,那么

//处置。

如果(读者!= null)

{

//弃置它。

((IDisposable)读者)。分散();

}

}

这应该编译得很好。有可能你错过了对变量声明的null

赋值。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam .guard.caspershouse.com


" Chris" <章*** @ discussions.microsoft.com>在留言中写道

新闻:FF ********************************** @ microsof t.com ...
Chris,

You should be doing something like this:

// Delcare the reader.
SqlDataReader reader = null;

// Use the reader here.
try
{
// Create the reader.
reader = new SqlDataReader(...);

// Use the reader.
}
catch
{
}
finally
{
// Check the reader for null. If it is not, then
// dispose.
if (reader != null)
{
// Dispose of it.
((IDisposable) reader).Dispose();
}
}

This should compile just fine. Chances are you missed the null
assignment to the variable declaration.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris" <Ch***@discussions.microsoft.com> wrote in message
news:FF**********************************@microsof t.com...


我在代码的很多地方使用try catch finally块。

当创建时一个sqldatareader博士,并尝试在我的
最后块中关闭它我得到错误:使用无符号的局部变量。

dr.close();
在尝试中工作正常但不是最终的。

如何在没有这些错误的情况下访问我的本地对象?

谢谢

Chris
Hi,

I am using the try catch finally block in many places in my code.

When a create an sqldatareader dr and try to close it in my
finally block I get the error: use of unsigned local variable.

dr.close();
works fine in the try but not in the finally.

How can I access my local objects in the finally without these errors?

thanks

Chris



看起来变量dr在try块中声明 - dr只在本地

尝试{}并且在finally块中无法访问。对于你的情况,decalre博士

在try块之外,这样你就可以同时访问它,以及

finally块。


克里斯 <章*** @ discussions.microsoft.com>在留言中写道

新闻:FF ********************************** @ microsof t.com ...




我在代码的很多地方使用try catch finally块。

当创建一个sqldatareader博士并尝试在我的

finally块中关闭它时,我得到错误:使用无符号局部变量。


dr.close();

在try中工作正常,但在finally中没有。


如何在没有这些的情况下访问finally中的本地对象错误?


谢谢


Chris
Looks like the variable dr is declared inside try block - dr is only local
to try {} and not accessible in finally block. For your case, decalre dr
outside the try block so that you can access it in both try as well as
finally blocks.

"Chris" <Ch***@discussions.microsoft.com> wrote in message
news:FF**********************************@microsof t.com...
Hi,

I am using the try catch finally block in many places in my code.

When a create an sqldatareader dr and try to close it in my
finally block I get the error: use of unsigned local variable.

dr.close();
works fine in the try but not in the finally.

How can I access my local objects in the finally without these errors?

thanks

Chris


" Chris" <章*** @ discussions.microsoft.com>在留言中写道

新闻:FF ********************************** @ microsof t.com ...
"Chris" <Ch***@discussions.microsoft.com> wrote in message
news:FF**********************************@microsof t.com...


我在代码的很多地方使用try catch finally块。

当创建时一个sqldatareader博士,并尝试在我的
最后块中关闭它我得到错误:使用无符号的局部变量。

dr.close();
在尝试中工作正常但不是最终的。

如何在没有这些错误的情况下访问我的本地对象?
Hi,

I am using the try catch finally block in many places in my code.

When a create an sqldatareader dr and try to close it in my
finally block I get the error: use of unsigned local variable.

dr.close();
works fine in the try but not in the finally.

How can I access my local objects in the finally without these errors?




你好Chris,

除了到目前为止给出的优秀答案外,还有另外一个你可能不知道的C#构造。




错误处理期间,您显然是一个好公民并清理数据阅读器。但是,C#有一个非常好的构造,它将为你调用

..Dispose方法......它被称为''使用''


我从随机网站中获取了一个使用数据读取器对象的错误示例

并重新编写它以使用''using''语句,这里:


SqlDataReader数据;

使用(data = command.ExecuteReader(CommandBehavior.CloseConnect ion))

{

while(data.Read())


{

Console.WriteLine(" Company Name" +

data.GetString(data.GetOrdinal(" CompanyName") ;));


}

} //自动调用data.Dispose();


这是MSDN中的规范页面。这是一种非常酷的

语言功能,可以很好地减少你的代码。

注意:你还需要一个试一试 - 最后,但你不需要关闭

数据阅读器,因为留下了使用代码块,出于任何原因,

将为您调用Dispose。

http://msdn.microsoft.com/library/de...pspec_8_13.asp

-

--- Nick Malik [微软]

MCSD,CFPS,认证Scrummaster
http://blogs.msdn.com/nickmalik


免责声明:意见在这个论坛上表达的是我自己的,而不是我雇主的b $ b代表。

我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。

-



Hi Chris,

In addition to the excellent answers given so far, there is another
construct of C# that you may not be aware of.

You are clearly being a good citizen and cleaning up the data reader during
error handling. However, C# has a pretty nice construct that will call the
..Dispose method for you... it is called ''using''

I snatched a bad example of using the Data Reader object from a random site
and rewrote it to use the ''using'' statement, here:

SqlDataReader data;
using (data = command.ExecuteReader(CommandBehavior.CloseConnect ion))
{
while( data.Read() )

{
Console.WriteLine("Company Name " +
data.GetString(data.GetOrdinal("CompanyName"));

}
} // automatically calls data.Dispose();

Here is the spec page in the MSDN. It''s a very cool capability of the
language, and reduces your code brilliantly.
Note: you still need a try-catch-finally, but you won''t need to close the
data reader, because leaving that block of ''using'' code, FOR ANY REASON,
will call Dispose for you.

http://msdn.microsoft.com/library/de...pspec_8_13.asp
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--


这篇关于终于声明和对象的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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