初级VB.NET问题 [英] Beginner level VB.NET question

查看:72
本文介绍了初级VB.NET问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是一名C#人员,负责修复VB.NET代码中的一些错误。我已经把

投入了一些似乎没有意义的事情。我有一个Object

对System.Windows.Forms.ToolStripComboBox对象的引用,它初始化了

并且执行得很好。现在,当关闭应用程序时,这个

对象获得Disposed。我正在尝试在调用其中一个属性之前检查此对象是否为null

(Nothing,我推测)。好像很简单,但是Nothing的测试总是失败,声称它不是

没什么。然后尝试访问其中一个属性结果

NullReferenceException;(我是否正确检查我的引用对象是否为
null?


示例:

''全局范围

Dim cB As ToolStripComboBox

..

..

''初始化和其他好东西

..

..

''Futhur在它自己的Sub下来

..

如果不是cB什么都没有

cB.SelectedIndex = 1

结束如果

解决方案

因此代码似乎没问题,但我只是想查一下,就像你提到的那样

是一个C#程序员.VB不区分大小写。所以你只是将

对象设置为代码中的任何地方,但没有意识到它是相同的

对象?


-

Rgds,

Anand

VB.NET MVP
htt p://www.dotnetindia.com

" OregonMike"写道:


>


我是一名C#人员,负责修复VB.NET代码中的一些错误。我已经把

投入了一些似乎没有意义的事情。我有一个Object

对System.Windows.Forms.ToolStripComboBox对象的引用,它初始化了

并且执行得很好。现在,当关闭应用程序时,这个

对象获得Disposed。我正在尝试在调用其中一个属性之前检查此对象是否为null

(Nothing,我推测)。好像很简单,但是Nothing的测试总是失败,声称它不是

没什么。然后尝试访问其中一个属性结果

NullReferenceException;(我是否正确检查我的引用对象是否为
null?


示例:

''全局范围

Dim cB As ToolStripComboBox

..

..

''初始化和其他好东西

..

..

''Futhur在它自己的Sub下来

..

如果不是cB什么都没有

cB.SelectedIndex = 1

结束如果


09月27下午1:45,OregonMike< OregonM ... @ gmail.comwrote:

我是C#的人,负责修复VB.NET代码中的一些错误。我已经把

运行到看似没有意义的东西。我有一个对象

对System.Windows.Forms.ToolStripComboBox对象的引用,该对象初始化为
并且执行得很好。现在,当关闭应用程序时,这个

对象获得Disposed。我正在尝试在调用其中一个属性之前检查此对象是否为null

(Nothing,我推测)。好像很简单,但是Nothing的测试总是失败,声称它不是

没什么。然后尝试访问其中一个属性结果

NullReferenceException;(我是否正确检查我的引用对象是否为
null?


示例:

''全局范围

Dim cB As ToolStripComboBox





''初始化和其他好东西





''Futhur在它自己的Sub下面



如果不是cB什么都没有

cB.SelectedIndex = 1

结束如果



这是正确的做法。你是否在这段代码运行之前检查了调试器中的cB

引用?

调试器说什么?


9月27日下午12:10,Anand [MVP]< Anand ... @ discussion.microsoft .com>

写道:


因此代码似乎没问题,但我只想查一下,就像你提到的那样

是一个C#程序员。 VB不区分大小写。所以你只是将

对象设置为代码中的任何地方,但没有意识到它是相同的

对象?


-

Rgds,

Anand

VB.NET MVPhttp://www.dotnetindia.com



" OregonMike"写道:


我是一个C#人员,负责修复VB.NET代码中的一些错误。我已经把

投入了一些似乎没有意义的事情。我有一个Object

对System.Windows.Forms.ToolStripComboBox对象的引用,它初始化了

并且执行得很好。现在,当关闭应用程序时,这个

对象获得Disposed。我正在尝试在调用其中一个属性之前检查此对象是否为null

(Nothing,我推测)。好像很简单,但是Nothing的测试总是失败,声称它不是

没什么。然后尝试访问其中一个属性结果

NullReferenceException;(我是否正确检查我的引用对象是否为
null?

< blockquote class =post_quotes>
例如:

''全局范围

Dim cB As ToolStripComboBox

..

..

''初始化和其他好东西

..

..

''Futhur在它自己的Sub

..

如果不是cB什么都没有

cB.SelectedIndex = 1

结束如果



对象在其他任何地方都没有设置为Nothing。我想知道是否

对象处于不一致状态或调试器有问题。




I''m a C# guy tasked with fixing some bugs in VB.NET code. I''ve run
into something that doesn''t seem to make sense. I have an Object
reference to a System.Windows.Forms.ToolStripComboBox Object that gets
initialized and performs just fine. Now, when closing the app, this
object gets Disposed. I''m trying to check whether this object is null
(Nothing, I presume) before calling one of its Properties. It seems
pretty simple, but the test for Nothing always fails claiming it''s not
Nothing. Then trying to access one of its Properties results in an
NullReferenceException ;( Am I checking whether my reference Object is
null correctly?

Example:
'' global scope
Dim cB As ToolStripComboBox
..
..
'' Initialization and other good stuff
..
..
'' Futhur down in its own Sub
..
If Not cB Is Nothing
cB.SelectedIndex = 1
End If

解决方案

As such the code seems OK, but I just want to check, as you did mention you
were a C# programmer. VB is not case sensitive. So are you just setting the
object to nothing somewhere in the code, but not realizing it is the same
object?

--
Rgds,
Anand
VB.NET MVP
http://www.dotnetindia.com
"OregonMike" wrote:

>

I''m a C# guy tasked with fixing some bugs in VB.NET code. I''ve run
into something that doesn''t seem to make sense. I have an Object
reference to a System.Windows.Forms.ToolStripComboBox Object that gets
initialized and performs just fine. Now, when closing the app, this
object gets Disposed. I''m trying to check whether this object is null
(Nothing, I presume) before calling one of its Properties. It seems
pretty simple, but the test for Nothing always fails claiming it''s not
Nothing. Then trying to access one of its Properties results in an
NullReferenceException ;( Am I checking whether my reference Object is
null correctly?

Example:
'' global scope
Dim cB As ToolStripComboBox
..
..
'' Initialization and other good stuff
..
..
'' Futhur down in its own Sub
..
If Not cB Is Nothing
cB.SelectedIndex = 1
End If


On Sep 27, 1:45 pm, OregonMike <OregonM...@gmail.comwrote:

I''m a C# guy tasked with fixing some bugs in VB.NET code. I''ve run
into something that doesn''t seem to make sense. I have an Object
reference to a System.Windows.Forms.ToolStripComboBox Object that gets
initialized and performs just fine. Now, when closing the app, this
object gets Disposed. I''m trying to check whether this object is null
(Nothing, I presume) before calling one of its Properties. It seems
pretty simple, but the test for Nothing always fails claiming it''s not
Nothing. Then trying to access one of its Properties results in an
NullReferenceException ;( Am I checking whether my reference Object is
null correctly?

Example:
'' global scope
Dim cB As ToolStripComboBox
.
.
'' Initialization and other good stuff
.
.
'' Futhur down in its own Sub
.
If Not cB Is Nothing
cB.SelectedIndex = 1
End If

That''s the correct way of doing it. Have you examined the cB
reference in the debugger just before the this piece of code runs?
What does the debugger say?


On Sep 27, 12:10 pm, Anand[MVP] <Anand...@discussions.microsoft.com>
wrote:

As such the code seems OK, but I just want to check, as you did mention you
were a C# programmer. VB is not case sensitive. So are you just setting the
object to nothing somewhere in the code, but not realizing it is the same
object?

--
Rgds,
Anand
VB.NET MVPhttp://www.dotnetindia.com

"OregonMike" wrote:

I''m a C# guy tasked with fixing some bugs in VB.NET code. I''ve run
into something that doesn''t seem to make sense. I have an Object
reference to a System.Windows.Forms.ToolStripComboBox Object that gets
initialized and performs just fine. Now, when closing the app, this
object gets Disposed. I''m trying to check whether this object is null
(Nothing, I presume) before calling one of its Properties. It seems
pretty simple, but the test for Nothing always fails claiming it''s not
Nothing. Then trying to access one of its Properties results in an
NullReferenceException ;( Am I checking whether my reference Object is
null correctly?

Example:
'' global scope
Dim cB As ToolStripComboBox
..
..
'' Initialization and other good stuff
..
..
'' Futhur down in its own Sub
..
If Not cB Is Nothing
cB.SelectedIndex = 1
End If

The object isn''t set to Nothing anywhere else. I''m wondering if the
object is in an inconsistent state or if the debugger is buggy.


这篇关于初级VB.NET问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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