Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 838447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:18:24+00:00 2026-05-15T05:18:24+00:00

In C# I can test for this… public event EventHandler Trigger; protected void OnTrigger(EventArgs

  • 0

In C# I can test for this…

public event EventHandler Trigger;
protected void OnTrigger(EventArgs e)
{
    if (Trigger != null)
        Trigger(this, e);
}

Is there a way to do this in VB.NET? Test for null I mean?

MORE INFO

I forgot to mention. I have classes written in C# but I am writing my unit tests in VB.NET.

I am trying this in the unit test…

If myObject.Trigger IsNot Nothing Then  
    ''#do something
End If

This is causing a compile time error which says … “Public Event Trigger is an Event and cannot be called directly. Use the RaiseEvent statement to raise an event.”

Seth

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:18:25+00:00Added an answer on May 15, 2026 at 5:18 am

    There is an interesting discussion in question 1129517 around how to do this very thing in C#.

    Since the class that contains the Event was written in C#, the delegate semantics do apply, and those techniques should work for you. However, you’ll need to translate the source to VB.NET for your unit test.

    Given the following class in a C# assembly:

    public class Triggerific
    {
        public event EventHandler Trigger;
    
        private static void OnTriggerTriggered(object sender, EventArgs e)
        {
            Console.WriteLine("Triggered!");
        }
    
        public void AddTrigger()
        {
            Trigger += OnTriggerTriggered;
        }
    }
    

    Here is some VB.NET code which will correctly determine if a handler was registered for the Trigger event:

    <TestMethod()> _
    Public Sub TriggerTest()
        Dim cut As New Triggerific
        cut.AddTrigger()
    
        Assert.IsNotNull(GetEventHandler(cut, "Trigger"))
    End Sub
    
    Private Shared Function GetEventHandler(ByVal classInstance As Object, ByVal eventName As String) As EventHandler
        Dim classType As Type = classInstance.[GetType]()
        Dim eventField As FieldInfo = classType.GetField(eventName, BindingFlags.GetField Or BindingFlags.NonPublic Or BindingFlags.Instance)
    
        Dim eventDelegate As EventHandler = DirectCast(eventField.GetValue(classInstance), EventHandler)
    
        ' eventDelegate will be null/Nothing if no listeners are attached to the event
        Return eventDelegate
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can this test fail? [TestMethod] public void Get_Code() { var expected = new
Hello I'm writting junit test how can I test this method .. this is
Somehow, using linq I can't test it with this CUF field in the beginning:
How can I get this test to pass without resorting to runs/waitsFor blocks? it(cannot
How can it be that this test case import unittest class PropTest(unittest.TestCase): def test(self):
I'm having a problem getting this test case to work. Can anyone point me
This is a weird one, and unfortunately I can't test it. At my company
Can someone tell me why this unit test that checks for exceptions fails? Obviously
I can't figure this out as when I test my rules at: http://htaccess.madewithlove.be/ It
Can anyone tell me why this code would not be working? $('body').on('test', function() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.