Beginner level VB.NET question

  • Thread starter Thread starter OregonMike
  • Start date Start date
O

OregonMike

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?
 
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?
 
Anand said:
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

What we are not seeing is how the cB variable is set. You have a
declaration and then a test. Nothing to set the value or to clear it that
we can see.


LS
 
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?

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.
 
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?


The debugger claims it's an object of the Type I'm expecting. If I
delve deeper into it, and check the SelectedIndex value it reads:

- cB {System.Windows.Forms.ToolStripComboBox}
System.Windows.Forms.ToolStripComboBox
+ SelectedIndex {"Object reference not set to an instance of an
object."} Integer

I'm a little baffled by this.
 
OregonMike said:
The debugger claims it's an object of the Type I'm expecting. If I
delve deeper into it, and check the SelectedIndex value it reads:

- cB {System.Windows.Forms.ToolStripComboBox}
System.Windows.Forms.ToolStripComboBox
+ SelectedIndex {"Object reference not set to an instance of an
object."} Integer

I'm a little baffled by this.

You have a ToolStripComboBox object all right. The problem could be that
the selecteded index cannot be set possibly because 1 exceeds the number of
items in the combobox. Check the number of items. It is a zero based
collection.

LS
 
The debugger claims it's an object of the Type I'm expecting. If I
delve deeper into it, and check the SelectedIndex value it reads:

- cB {System.Windows.Forms.ToolStripComboBox}
System.Windows.Forms.ToolStripComboBox
+ SelectedIndex {"Object reference not set to an instance of an
object."} Integer

I'm a little baffled by this.

That's pretty weird. It looks like the SelectedIndex property is
what's generating the exception. I say it's weird because that
property is not suppose to throw an exception...at least according to
the documentation anyway.

Can you post the least amount of code required to replicate the
problem?
 
You have a ToolStripComboBox object all right. The problem could be that
the selecteded index cannot be set possibly because 1 exceeds the number of
items in the combobox. Check the number of items. It is a zero based
collection.

I tested that scenario. It throws an ArgumentOutOfRange exception.
 
Brian Gideon said:
That's pretty weird. It looks like the SelectedIndex property is
what's generating the exception. I say it's weird because that
property is not suppose to throw an exception...at least according to
the documentation anyway.

Can you post the least amount of code required to replicate the
problem?
I'm not sure if I'm right, but I think the item at position 1 of the
combobox.objectcollection is causing the problem. Because this item was not
declared as a proper object you'll receive this exception as an inner
exception. Maybe you can try something like this:

'I prefer to use IsNot Nothing cause it's easier to read for me. :-)
If cB IsNot Nothing
dim mytest as new object
mytest = cb.items.item(0)
'If this line causes an error, please use the debugger to view
cb.items.item(1)
mytest = cb.items.item(1)
cB.SelectedIndex = 1
End If
 
That's pretty weird. It looks like the SelectedIndex property is
what's generating the exception. I say it's weird because that
property is not suppose to throw an exception...at least according to
the documentation anyway.

Can you post the least amount of code required to replicate the
problem?

I'm coming to the conclusion that due to the Disposing that the Object
is in an inconsistent state and thus the disclaimers of working on an
object after its Disposed. The fact that it looks like it's not
Disposed is what is throwing me off ;(
 
This code will throw an ArgumentNullException:

comboBox1.Items.Add("Fred");
comboBox1.Items.Add("Barney");
comboBox1.Items.Add("Wilma");
comboBox1.Items[1] = null;
comboBox1.SelectedIndex = 1;

I would break before you set the selectedindex and see what object is at
items [1].
 
Oregon Mike,

This would probably give exactly the same error in C#

if (!CB == null)
{
cb.SelectedIndex = 1;
}

Are you sure that you want to select the second item?

Cor
 
For those who see this it should be
if (CB != null)

Just to much thinking in the same VB code it would be
if (!(CB == null))

What nice is VB to show it so clear

:-)

Cor
 
This code will throw an ArgumentNullException:

comboBox1.Items.Add("Fred");
comboBox1.Items.Add("Barney");
comboBox1.Items.Add("Wilma");
comboBox1.Items[1] = null;
comboBox1.SelectedIndex = 1;

I would break before you set the selectedindex and see what object is at
items [1].

Well, that's not the same exception the OP was getting, but it's
interesting nonetheless. What line generated the exception? Last?
Second to last?
 
What we are not seeing is how the cB variable is set. You have a
declaration and then a test. Nothing to set the value or to clear it that
we can see.

LS


LS,

Yes, I just gave a brief example. The Object is initialized and has
many of its Properties set during the Form's Initialization process.
I went another route to avoid this issue. Strange stuff!
 
I'm not sure if I'm right, but I think the item at position 1 of the
combobox.objectcollection is causing the problem. Because this item was not
declared as a proper object you'll receive this exception as an inner
exception. Maybe you can try something like this:

'I prefer to use IsNot Nothing cause it's easier to read for me. :-)
If cB IsNot Nothing
dim mytest as new object
mytest = cb.items.item(0)
'If this line causes an error, please use the debugger to view
cb.items.item(1)
mytest = cb.items.item(1)
cB.SelectedIndex = 1
End If

Oliver,

I'm just using the value 1 as an example. The cB.items will also
appear as "Object reference not set to an instance of an
object." This isn't a simple argument range issue unfortunately.
 
Oregon Mike,

This would probably give exactly the same error in C#

if (!CB == null)
{
cb.SelectedIndex = 1;
}

Are you sure that you want to select the second item?

Cor

Cor,

The item selected is just an example. I can't even access
cB.SelectedIndex for reading.
It reports "Object reference not set to an instance of an
object." as if the Object cB is null (Nothing). This is strange stuff,
I went about solving the issue another way to avoid this dilemma.
 
Back
Top