By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

adding a menuitem array issue

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> schrieb
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

Nov 20 '05 #2

P: n/a
Eric Sabine wrote:
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) {}".

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #3

P: n/a
* "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> scripsit:
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'.
------ error below ------- here I get an "Object reference not set to an
instance of an object." error
myProgramsContextMenu1.MenuItems.AddRange(mic)


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4

P: n/a
D'OH!

Thanks Herfried, Sven, and Armin.

Eric
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:c7************@ID-208219.news.uni-berlin.de...
* "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> scripsit:
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'.
------ error below ------- here I get an "Object reference not set to an
instance of an object." error
myProgramsContextMenu1.MenuItems.AddRange(mic)


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.