VB.NET: WithEvents not Working
I have the following classes:
Public Class Email
Private Shared ReadOnly EMAIL_REGEX = "\b[a-zA-Z]+[a-zA-Z0-9._+-]+@" + _
"[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}\b"
Private _email As String
Public Event emailCreated()
' Declare empty constructor private so the only way to create an object
' is using new (email)
Private Sub New()
End Sub
Sub New(ByVal email As String)
If Regex.IsMatch(email, EMAIL_REGEX) Then
_email = email
RaiseEvent emailCreated()
Else
Throw New Exception("Email Not Valid")
End If
End Sub
ReadOnly Property Email() As String
Get
Return _email
End Get
End Property
End Class
And
Public Class Emai开发者_开发问答lForm
WithEvents myEmail As Email
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
myEmail = New Email(TextBox1.Text)
Catch ex As Exception
MessageBox.Show("Exception: " & ex.Message)
End Try
End Sub
Public Sub emailCreated() Handles myEmail.emailCreated
MessageBox.Show("New Email Created")
End Sub
End Class
If a create a wrong email lets say "email" the exception is correctly cached and a message is showed however is i input a valid email the event is not raised, the object is being created but no message is shown and no error or exception is thrown i suspect it has something to do with using "myemail = new Email(email)" but i have seen examples of using new with withevents with no problem. I would apreciate any input about this problem
thank you
Edit: for future reference - VB.NET - problem with member's event handling
You should not validate email addresses using regular expressions.
You should use the MailAddress
class instead:
Sub New(ByVal email As String)
New MailAddress(email).ToString()
End Sub
The MailAddress
class uses a BNF parser to validate the address, and will throw a FormatException
if the address is invalid.
Your regex will reject valid addresses like admin@mta.info
.
To answer your question, the event is being raised before the WithEvents
field has a value.
Here is what happens when your code is executed:
New Email(TextBox1.Text)
– TheEmail
object is created, and the constructor raises the event.
At this point, the event has no handlers, so the event doesn't do anything.myEmail = ...
The newEmail
object is assigned to themyEmail
field, and the event handler is added.
It is not possible to work around this problem, because you cannot get a reference to an object before its constructor finishes.
Instead, you should get rid of the event and make the myEmail
field into a property with your code in the property setter
First guess: an Email is created before events are hooked up to it. You're firing the event before you're even out of the constructor, before VB even has the opportunity to add event handlers, so the event will never be seen.
Bigger-picture view. Is there a particular reason you want an emailCreated event fired? It's looking like the event will never be handled outside your own class even if you end up getting it working, so using events adds complexity you may not need or even want.
精彩评论