AddressOf issue, can somebody explain this?

  • Thread starter Thread starter Eric Newton
  • Start date Start date
E

Eric Newton

first line fails with "Reference to a non-shared member requires an object
reference."
yet second line works... any reasons?

ctxMenu.MenuItems.Add("Add new pattern...", AddressOf
mtTemplateNode.cmdPatternAdd_Click)
ctxMenu.MenuItems.Add("Add new pattern...", New
System.EventHandler(AddressOf mtTemplateNode.cmdPatternAdd_Click))
 
the new in the second line creates an mtTemplateNode object
one does not exist in the first line

Hope this helps..
 
Eric,
What type of construct is mtTemplateNode?

Is it an object, a class, a form, a structure, a variable, something else?

If mtTemplateNode is the name of a class, you cannot (should not) add an
event at the class level to an instance method. You will get a null
reference exception when the event itself was raised.

Try
Dim node As New mtTemplateNode
ctxMenu.MenuItems.Add("Add new pattern...", AddressOf
node.cmdPatternAdd_Click)
ctxMenu.MenuItems.Add("Add new pattern...", New
System.EventHandler(AddressOf node.cmdPatternAdd_Click))

Notice that I am using an instance of the mtTemplateNode class for the event
handler.

Alternatively put Shared in front of the cmdPatternAdd_Click method.

Hope this helps
Jay

Eric Newton said:
first line fails with "Reference to a non-shared member requires an object
reference."
yet second line works... any reasons?

ctxMenu.MenuItems.Add("Add new pattern...", AddressOf
mtTemplateNode.cmdPatternAdd_Click)
ctxMenu.MenuItems.Add("Add new pattern...", New
System.EventHandler(AddressOf mtTemplateNode.cmdPatternAdd_Click))


--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]
 
Back
Top