adding a menuitem array issue

  • Thread starter Thread starter Eric Sabine
  • Start date Start date
E

Eric Sabine

I've simplified the code to the following. I'm just overlooking something
very simple I'm sure.

Dim mic() As MenuItem
mic = New MenuItem(3) {}
mic(0) = New MenuItem("a")
mic(0).Index = 1

mic(1) = New MenuItem("b")
mic(1).Index = 2

mic(2) = New MenuItem("c")
mic(2).Index = 3

mic(1).MenuItems.Add(mic(2))

------ error below ------- here I get an "Object reference not set to an
instance of an object." error
myProgramsContextMenu1.MenuItems.AddRange(mic)


What do you think is the cause of the error?

Eric
 
Eric Sabine said:
I've simplified the code to the following. I'm just overlooking
something very simple I'm sure.

Dim mic() As MenuItem
mic = New MenuItem(3) {}
mic(0) = New MenuItem("a")
mic(0).Index = 1

mic(1) = New MenuItem("b")
mic(1).Index = 2

mic(2) = New MenuItem("c")
mic(2).Index = 3

mic(1).MenuItems.Add(mic(2))

------ error below ------- here I get an "Object reference not set to
an instance of an object." error
myProgramsContextMenu1.MenuItems.AddRange(mic)


What do you think is the cause of the error?


In the mic array, the item with index=3 does not contain a reference to a
MenuItem.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Eric said:
I've simplified the code to the following. I'm just overlooking
something very simple I'm sure.

Dim mic() As MenuItem
mic = New MenuItem(3) {}
mic(0) = New MenuItem("a")
mic(0).Index = 1

mic(1) = New MenuItem("b")
mic(1).Index = 2

mic(2) = New MenuItem("c")
mic(2).Index = 3

mic(1).MenuItems.Add(mic(2))

------ error below ------- here I get an "Object reference not set to
an instance of an object." error
myProgramsContextMenu1.MenuItems.AddRange(mic)

What do you think is the cause of the error?

VB.NET is not C# in it's array declarations. The statement "mic = New
MenuItem(3) {}" creates an array of length *4*, that is 0 to 3 *inclusive*.
The error is caused when AddRange tries to add mic(3), which is still
Nothing. You need to change your assignment to "mic = New MenuItem(2) {}".
 
* "Eric Sabine said:
I've simplified the code to the following. I'm just overlooking something
very simple I'm sure.

Dim mic() As MenuItem
mic = New MenuItem(3) {}
mic(0) = New MenuItem("a")
mic(0).Index = 1

mic(1) = New MenuItem("b")
mic(1).Index = 2

mic(2) = New MenuItem("c")
mic(2).Index = 3

mic(1).MenuItems.Add(mic(2))

'mic(3)' is still 'Nothing'.
 
Back
Top