ThisIsABird ThisIsABird - 2 years ago 68 Question - Can't generate the custom event

I'm a little confused about the problem in this class. I want the Pay Button to generate a custom event. I am using Visual Basic Studio 2010 in .NET I think the main problem is within the RaiseEvent line.

Public Class Payment
Public Delegate Sub PaymentMadeEvent(ByVal sender As Object, ByVal e As PaymentMadeEventArgs)
Public Event PaymentEvent As PaymentMadeEvent

Private Property __paymentAmount As Decimal
Public Property paymentAmount As Decimal
Return __paymentAmount
End Get
Set(ByVal value As Decimal)
__paymentAmount = value
PaymentBox.Text = String.Format("{0:c}", __paymentAmount)
End Set
End Property

Private Sub PayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayButton.Click
RaiseEvent PaymentEvent(Me, New PaymentMadeEventArgs() {paymentSuccess = True})
End Sub

Public Class PaymentMadeEventArgs : Class EventArgs
Private Property __paymentSuccess As Boolean
Public Property paymentSuccess As Boolean
Return __paymentSuccess
End Get
Set(ByVal value As Boolean)
__paymentSuccess = value
End Set
End Property
End Class
End Class
End Class

Anyways, Advance thanks!

Answer Source

There are several errors/bad coding styles in your code.

  1. Private Property __paymentAmount As Decimal

You don´t need a private property, just use a variable. Also don´t use __ as prefix. Use single _ underscore or no prefix:

Private _paymentAmount As Decimal
  1. Public Property paymentAmount As Decimal

Start public property names with a capital:

Public Property PaymentAmount As Decimal
  1. Public Class PaymentMadeEventArgs : Class EventArgs

This is not the correct VB .NET syntax for inheritance. Additionally use a Auto-Property for PaymentSuccess less code and better reading:

  Public Class PaymentMadeEventArgs
        Inherits EventArgs
        Public Property PaymentSuccess As Boolean

  End Class

After you fixed all of that raise your custom event in PayButton_Click:

RaiseEvent PaymentEvent(Me, New PaymentMadeEventArgs() With {.PaymentSuccess = True})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download