分配前使用的对象 [英] object used before assigned

查看:65
本文介绍了分配前使用的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS 2008中,


我有一个对象dbReader,我得到一个警告说它在被分配之前被使用了

价值。


这是正确的。


Dim dbReader作为SqlDataReader


及更高版本:


最后

如果(不是dbReader什么都没有)那么dbReader.Close()

结束尝试


为什么这是一个错误?


这里我正在测试它是否是什么,它是什么(不是吗?)

使用它。该错误在if中。测试不是dbReader.Close()。


谢谢,


Tom

In VS 2008,

I have an object, dbReader, that I get a warning saying that it is used
before it has been assigned a value.

That is correct.

Dim dbReader As SqlDataReader

and later:

Finally
If (Not dbReader Is Nothing) Then dbReader.Close()
End Try

Why is this an error?

Here I am testing to see if it is nothing, which it is (isn''t it?) before
using it. The error is in the "if" test not the dbReader.Close().

Thanks,

Tom

推荐答案

2008年10月6日星期一08:11:32 -0700,tshad < tf*@dslextreme.comwrote:
On Mon, 6 Oct 2008 08:11:32 -0700, "tshad" <tf*@dslextreme.comwrote:

>在VS 2008中,

我有一个对象dbReader,我得到了一个在分配值之前警告说它已被使用

这是正确的。


Dim dbReader作为SqlDataReader

以后:


最后

如果(不是dbReader什么都没有)那么dbReader.Close()

结束试试

为什么这是一个错误?

在这里我要测试它是否是什么,它是什么(不是吗?)之前
使用它。该错误在if中。测试不是dbReader.Close()。

谢谢,

Tom
>In VS 2008,

I have an object, dbReader, that I get a warning saying that it is used
before it has been assigned a value.

That is correct.

Dim dbReader As SqlDataReader

and later:

Finally
If (Not dbReader Is Nothing) Then dbReader.Close()
End Try

Why is this an error?

Here I am testing to see if it is nothing, which it is (isn''t it?) before
using it. The error is in the "if" test not the dbReader.Close().

Thanks,

Tom



我认为错误是正确的。编译器抱怨你在将dbReader设置为任何东西之前测试dbReader。


你假设dbReader初始化为Nothing因此

测试没问题。虽然这可能是真的,但您应该明确地将D / b
设置为Dim行上的Nothing。编译器在设置之前检查

不考虑

未初始化变量的值。

I think the error is correct. The compiler is complaining that you
are testing dbReader before you have set it to anything.

You are assuming that dbReader is intialized to Nothing and therefore
the test is OK. While that is probably true, you should explicitly
set dbReader to Nothing on the Dim line. The compiler checks for
using before setting do not take into account the value that an
uninitialized variable has.


Dim dbReader As SqlDataReader = Nothing


试试吧。


" tshad" < tf*@dslextreme.com写信息

新闻:OG ************** @ TK2MSFTNGP02.phx.gbl ...
Dim dbReader As SqlDataReader = Nothing

Try that.


"tshad" <tf*@dslextreme.comwrote in message
news:OG**************@TK2MSFTNGP02.phx.gbl...

在VS 2008中,


我有一个对象dbReader,我得到一个警告说它已被使用

之前已经分配了一个值。


这是正确的。


Dim dbReader作为SqlDataReader


及以后:


最后

如果(不是dbReader什么都没有)那么dbReader.Close()

结束尝试


为什么这是一个错误?


我在这里测试看它是不是什么,它是什么(不是吗) ?)之前使用它来获得
。该错误在if中。测试不是dbReader.Close()。


谢谢,


Tom
In VS 2008,

I have an object, dbReader, that I get a warning saying that it is used
before it has been assigned a value.

That is correct.

Dim dbReader As SqlDataReader

and later:

Finally
If (Not dbReader Is Nothing) Then dbReader.Close()
End Try

Why is this an error?

Here I am testing to see if it is nothing, which it is (isn''t it?) before
using it. The error is in the "if" test not the dbReader.Close().

Thanks,

Tom



" Jack Jackson" < jj ****** @ cinnovations.netschrieb
"Jack Jackson" <jj******@cinnovations.netschrieb

On Mon,2008年10月6日08:11:32 -0700," tshad" < tf*@dslextreme.com>

写道:
On Mon, 6 Oct 2008 08:11:32 -0700, "tshad" <tf*@dslextreme.com>
wrote:

在VS 2008中,


我有一个对象dbReader,我得到一个警告,说它在分配值之前使用了




这是正确的。


Dim dbReader作为SqlDataReader


及更高版本:


最后

If(不是dbReader什么都没有)然后dbReader.Close()

结束尝试


为什么这是一个错误?


在这里,我正在测试是否它是什么,它是(不是吗?)

使用它之前。该错误在if中。测试不是

dbReader.Close()。


谢谢,


Tom
In VS 2008,

I have an object, dbReader, that I get a warning saying that it is
used before it has been assigned a value.

That is correct.

Dim dbReader As SqlDataReader

and later:

Finally
If (Not dbReader Is Nothing) Then dbReader.Close()
End Try

Why is this an error?

Here I am testing to see if it is nothing, which it is (isn''t it?)
before using it. The error is in the "if" test not the
dbReader.Close().

Thanks,

Tom



我认为错误是正确的。编译器抱怨你在将dbReader设置为任何东西之前测试dbReader。


你假设dbReader初始化为Nothing并且

因此测试没问题。虽然这可能是真的,但你应该在Dim行显式地将dbReader设置为Nothing。编译器

在设置之前检查使用时不要考虑

未初始化变量的值



I think the error is correct. The compiler is complaining that you
are testing dbReader before you have set it to anything.

You are assuming that dbReader is intialized to Nothing and
therefore the test is OK. While that is probably true, you should
explicitly set dbReader to Nothing on the Dim line. The compiler
checks for using before setting do not take into account the value
that an
uninitialized variable has.



我认为使用Nothing

初始化引用变量是一种不好的做法,只是为了抑制编译器警告。也许在Try块中他确实使用了

dbReader.method并且忘了初始化变量,我的意思是

将它设置为真实对象而不是Nothing。它几乎就像禁用

Option Strict以避免在特定情况下永远不会导致

异常的行中的警告但可以让我们忽略其他的错误
个案。


我也认为Is / IsNot参考变量的比较不应该导致编译器警告。

Armin

I think it is a bad practice to initialize a reference variable with Nothing
just to suppress the compiler warning. Maybe in the Try block he does use
dbReader.method and did forget to initialize the variable, and I mean
setting it to a real object and not to Nothing. It is almost like disabling
Option Strict to avoid warnings in lines that will never lead to an
exception in the particular case but can make us overlook errors in other
cases.

I also think that Is/IsNot comparisons of reference variables should not
lead to a compiler warning.
Armin


这篇关于分配前使用的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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