Question Handles clause requires a WithEvents variable..why?

yuripace

Member
Joined
Dec 1, 2010
Messages
5
Programming Experience
3-5
Hi,
i'm a .net programmer, using .net framework 4, and i have a question about WithEvents clause.

i created my base class

VB.NET:
public class clsbaseclass
    public property p1 as string
    public property p2 as string
end class

and a custom bindinglist class with some methods inside

VB.NET:
public class lstBaseClass
   inherits BindingList(of clsBaseClass)

   ...sub fill()
  .. and other methods
end class

i declared a global variable

VB.NET:
public withevents mylist as new lstBaseClass

but when i try to access to addingnew events of the bindinglist(of t) class

VB.NET:
Private Sub mylist_AddingNew(ByVal sender As Object, ByVal e As AddingNewEventArgs) Handles mylist.AddingNew

i get this error:

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

I think the problem is my custom bindinglist class....but how can i solve this?

many thanks!!!

Acquistapace Yuri
 
Last edited:
Handles must refer to WithEvents variable in same class.
 
"in same class" means "defined in the containing type".
 
if i declare mylist variable in this way works..why?

public withevents mylist as new bindinglist(of clsBaseClass)

you mean that i have to define the events in my clsbaseclass?

sorry for question but it's the first time i manage events :)
 
If you define WithEvents mylist for example in a form class you can use 'Handles mylist' event handler in same form class also.
The type of the variable has no relevance, but it must of course be a type that has events.
In your first post lstBaseClass class that class has events inherited from the base class so that is no problem.

Don't use the word 'Class' in your class name unless it has a specific meaning beside being a class type. Use a meaningful names.
 
As I said, derived classes inherit base events. This has nothing to do with the fact that Handles statement requires the WithEvents variable to be declared in same class as event handler.
See for example WithEvents and the Handles Clause
 
well..i think i understand, but no changes in my code, still error, so i think i'm doing something wrong..

thanks for patience!

edit: i understand why my code is not working and i'm working to find the solution..a big thanks for your support !!!!
 
Last edited:
Back
Top