how to know whether the event make the instance from the delegate or not???

lord_of_pop

New member
Joined
Jul 24, 2006
Messages
3
Programming Experience
Beginner
hi guys
i hope to find the solution with u
iam trying to convert some code blocks from c# to vb.net
in C# u can make the event instance = null
and u can ask in the code wether it makes the instance or not
like this
public delegate void ChangePageEventHandler(object sender, frmMain.Pages Page);
public event ChangePageEventHandler ChangePage = null;


public void OnChangePage(frmMain.Pages Page)
{
if (this.ChangePage != null)
{
this.ChangePage(this, Page);
}
}

but in vb.net u cant assign "=nothing " to the event decleration
and u cant put the event in if statement to know wether it made the instance or not coz the compiler
tells u "this is event , use Raiseevent to call it"
plz help me in this
thanks
 
Done a quick conversion, this is how the vb.net would look, and how to check to see if there are any attached event handlers.

VB.NET:
Public Delegate Sub ChangePageEventHandler(ByVal sender As Object, ByVal Page As frmMain.Pages)
Public Event ChangePage As ChangePageEventHandler
 
Public Sub OnChangePage(ByVal Page As frmMain.Pages)
If Not Me.ChangePageEvent Is Nothing Then
RaiseEvent ChangePage(Me, Page)
End If
End Sub
 
reply

hi thanks for trying
but its not the matter of converting coz i know how to convert it
but the matter is how to know event obeject is having an instance or not
 
lord_of_pop, you got the answer but didn't see it, the declaration of your 'ChangePage' event emits a private field 'ChangePageEvent' which is the delegate instance of this event. As vis781 said you can check if this is Nothing. VB.Net also implicitly defines the delegate 'ChangePageEventHandler' when you declare an event, so you can leave out the delegate declaration. Also see documentation for Delegate that explains about the invocation list. Example:
VB.NET:
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
  Dim d As New delev
  AddHandler d.ChangePage, AddressOf dlgMethodExternal
  d.raise()
End Sub
 
Private Sub dlgMethodExternal() 
  MsgBox("hi external")
End Sub
 
[COLOR=blue]Class delev[/COLOR]
Public Event ChangePage()
 
Private Sub dlgMethodInternal()
  MsgBox("hi internal")
  For Each dl As [Delegate] In ChangePageEvent.GetInvocationList
    MsgBox(dl.Method.Name)
  Next
End Sub
 
Public Sub New()
  AddHandler Me.ChangePage, AddressOf dlgMethodInternal 'try to comment this line out
  If Me.ChangePageEvent Is Nothing Then
    [COLOR=black]AddHandler Me.ChangePage, AddressOf dlgMethodInternal[/COLOR]
[SIZE=2][COLOR=black]    MsgBox([/COLOR][/SIZE][COLOR=black][SIZE=2]"added internal"[/SIZE][SIZE=2])[/SIZE][/COLOR]
[SIZE=2][COLOR=black]  Else[/COLOR][/SIZE]
[SIZE=2][COLOR=black]    [SIZE=2]MsgBox([/SIZE][SIZE=2]"not added internal twice"[/SIZE][SIZE=2])
[/SIZE][/COLOR][/SIZE]  End If
End Sub
 
Public Sub raise()
  RaiseEvent ChangePage()
End Sub
[COLOR=blue]End Class[/COLOR]
 
Back
Top