{"id":1563,"date":"2009-09-25T13:10:00","date_gmt":"2009-09-25T13:10:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2009\/09\/25\/testing-events-for-nothingnull-doug-rothaus\/"},"modified":"2024-07-05T12:46:48","modified_gmt":"2024-07-05T19:46:48","slug":"testing-events-for-nothingnull-doug-rothaus","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/testing-events-for-nothingnull-doug-rothaus\/","title":{"rendered":"Testing Events for Nothing\/Null (Doug Rothaus)"},"content":{"rendered":"<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">While helping some Windows API folks with some sample code this week, I stumbled upon&#8230;uh&#8230;I mean &ldquo;carefully researched&rdquo; an issue that you might find handy.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">You may be aware that the <b>RaiseEvent<\/b> statement automatically checks to verify if an event handler exists before raising the event. If the event is <b>Nothing<\/b>, then there&rsquo;s no event handler and <b>RaiseEvent<\/b> terminates. If the event is not <b>Nothing<\/b>, then <b>RaiseEvent<\/b> triggers the event.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">However, what if you want to follow a different code path if the event is <b>Nothing<\/b>? Unfortunately, the <b>RaiseEvent<\/b> statement doesn&rsquo;t return a value that could indicate whether the event was fired. So, what&rsquo;s a developer to do?<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">The answer? When the VB compiler sets up your event, it creates a private variable for the event in the form of <i>&lt;event name&gt;Event<\/i>. So, if I set up an event named <\/font><span>TriggerMe<\/span><font face=\"Calibri\" size=\"3\">, the VB compiler creates an event variable named <\/font><span>TriggerMeEvent<\/span><font face=\"Calibri\" size=\"3\">.&nbsp;You can test this variable for <b>Nothing<\/b>. It&rsquo;s as simple as that! (Note: You won&#8217;t see this variable using Intellisense, but you can find it when debugging or through reflection.)<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Here&rsquo;s a quick example:<\/font><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Public<\/span> <span>Event<\/span> SolutionFound <span>As<\/span> EventHandler<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Private<\/span> <span>Sub<\/span> OnSolutionFound()<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>If<\/span> SolutionFoundEvent <span>IsNot<\/span> <span>Nothing<\/span> <span>Then<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>RaiseEvent<\/span> SolutionFound(<span>Me<\/span>, <span>New<\/span> EventArgs())<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Else<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8216; No event handler has been set.<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>MsgBox(<span>&#8220;There is no event handler. That makes me sad.&#8221;<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>If<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Sub<\/span><\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While helping some Windows API folks with some sample code this week, I stumbled upon&#8230;uh&#8230;I mean &ldquo;carefully researched&rdquo; an issue that you might find handy. You may be aware that the RaiseEvent statement automatically checks to verify if an event handler exists before raising the event. If the event is Nothing, then there&rsquo;s no event [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[59,166,167],"class_list":["post-1563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-doug-rothaus","tag-vb2008","tag-vb2010"],"acf":[],"blog_post_summary":"<p>While helping some Windows API folks with some sample code this week, I stumbled upon&#8230;uh&#8230;I mean &ldquo;carefully researched&rdquo; an issue that you might find handy. You may be aware that the RaiseEvent statement automatically checks to verify if an event handler exists before raising the event. If the event is Nothing, then there&rsquo;s no event [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/1563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=1563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/1563\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=1563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=1563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=1563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}