Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a List(Of T) in a RaiseEvent.

Status
Not open for further replies.

VB4Life

Programmer
Nov 8, 2008
50
US
I am trying to pass a List(Of Thing) thru a public event from a UserControl to my form. The object(Thing) is a class that has 4 variables that get populated in the UC, and I add to this List as there multiple Thing objects, I have checked this part they are filling properly. But when I RaiseEvent and try to pass the List thru the event I get a NullReferenceException during my mousedown event(formside) which does not have a problem if I comment out the RaiseEvent. I have tried the class as a nested class in the UC or just after the UC class - declared as public. Any ideas? Thanks for looking.
 
A little code could help
Have you added an instance (new) of the classes in the List(Of) ? Are they shared?
 
Well there was MUCH confusing code, what I did is change the events on the usercontrol to addhandlers on the main form and then the class moved to this form also which made life very simple, It just too hard to pass a List(Of T) in a Public Event for an object class that is located elsewhere. Thanks for getting back.
 
I at least dont have a clear view of the problem. So without some code i have to guess. I will say a few things and hopefully they will help.

First, i dont see any problem passing any variable (simple or complex-array) though an event. It all ends like passing an argument to a method (sub or function).

Ok, you can raise an event with RaiseEvent keyword. If the name is 'Event1' and assume that we pass one listof, then it should be declared like:
Code:
Public Event Event1(ByVal var_name As List(Of Thing))

And raise it like:
Code:
Dim li As New List(Of Thing)
' ... code ...
' li.Add(...)
RaiseEvent Event1(li)

In order to be able to catch the event, the host form of the user control should reference it with its events, like:
Code:
' This line should be in HostForm.Designer.vb
Friend WithEvents uc1 As UserControl1

Last, catch the event. Two ways:
1. Fixed by design time
Code:
Private Sub Event1Catcher(ByVal thing As List(Of Thing)) _
Handles uc1.Event1

End Sub

or 2. Having the above without the Handels. So at run time you have to add the handler. To add 'AddHandler' and to remove 'RemoveHandler' keywords.

--------------

Can you show us the raiseevent line and the code where you prepare the class to add it into the List(Of) ?
 
I am well adept at Public events and how to wire them - same format you used. This all might have worked fine, but I noticed in my controlRemoved event on the UC was dropping the last control without throwing an error in my (Try-Catch) this was now bypassing my public event - hence not firing. The solution I mentioned above fixed it as well and made it easier to populate the members of the class. Thanks again TipGiver.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top