变量和Try / Catch [英] Variables and Try/Catch
问题描述
如何访问try / catch块中设置的变量?
以下代码产生了使用未分配的局部变量错误:
string varOne;
try
{
//在这里访问数据库,选择,datareader等...
varOne = dtr [" column_name"]。ToString() ;
}
catch(例外e)
{
//东西
}
MessageBox.Show(varOne);
我想在try catch中包装数据库连接等,然后使用
变量在外面。
How do I access variables that are set within a try/catch block? The
following code produces a use of unassigned local variable error:
string varOne;
try
{
//access database here, select, datareader, etc...
varOne = dtr["column_name"].ToString();
}
catch (Exception e)
{
//something
}
MessageBox.Show(varOne);
I want to wrap the database connectivity, etc. in the try catch then use the
variable outside.
推荐答案
PS我还有一个与数据库服务器等断开连接的finally块。
如果模态msgbox仍处于打开状态,则不会发生这种情况。所以我想在最后的try之外使用
变量集。
" Jake K" < no ***** @ address.comkwrote in message
news:ek ************** @ TK2MSFTNGP06.phx.gbl ...
P.S. I also have a finally block that disconnects from the db server, etc.
That will not occur if the modal msgbox is still open. So I want to use the
variable set in the try outside of finally.
"Jake K" <no*****@address.comkwrote in message
news:ek**************@TK2MSFTNGP06.phx.gbl...
如何访问try / catch块中设置的变量?
以下代码产生了使用未分配的局部变量错误:
string varOne;
try
{
//在这里访问数据库,选择,datareader等...
varOne = dtr [" column_name"]。ToString() ;
}
catch(例外e)
{
//东西
}
MessageBox.Show(varOne);
我想在try catch中包装数据库连接等,然后使用
外面的变量。
How do I access variables that are set within a try/catch block? The
following code produces a use of unassigned local variable error:
string varOne;
try
{
//access database here, select, datareader, etc...
varOne = dtr["column_name"].ToString();
}
catch (Exception e)
{
//something
}
MessageBox.Show(varOne);
I want to wrap the database connectivity, etc. in the try catch then use
the variable outside.
" Jake K" < no ***** @ address.comkwrote in message
news:ek ************** @ TK2MSFTNGP06.phx.gbl ...
"Jake K" <no*****@address.comkwrote in message
news:ek**************@TK2MSFTNGP06.phx.gbl...
以下代码生成使用未分配的局部变量错误:
string varOne;
The following code produces a use of unassigned local variable error:
string varOne;
是的,它会...
string varOne = String.Empty;
现在它不会...... :-)
Yes, it will...
string varOne = String.Empty;
Now it won''t... :-)
谢谢。字符串varOne = null;与字符串varOne =
String.Empty相同?
" Mark Rae" < ma ** @ markNOSPAMrae.com写信息
新闻:uF ************* @ TK2MSFTNGP04.phx.gbl ...
Thanks. Is string varOne = null; the same as string varOne =
String.Empty?
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uF*************@TK2MSFTNGP04.phx.gbl...
" Jake K" < no ***** @ address.comkwrote in message
news:ek ************** @ TK2MSFTNGP06.phx.gbl ...
"Jake K" <no*****@address.comkwrote in message
news:ek**************@TK2MSFTNGP06.phx.gbl...
>以下代码生成使用未分配的局部变量错误:
string varOne;
>The following code produces a use of unassigned local variable error:
string varOne;
是的,它会...
string varOne = String.Empty;
现在它不会...... :-)
Yes, it will...
string varOne = String.Empty;
Now it won''t... :-)
这篇关于变量和Try / Catch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!