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 7669637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:38:26+00:00 2026-05-31T15:38:26+00:00

I have a method which calls an Action<string> public Action<string> DisplayError; public void MyMethod()

  • 0

I have a method which calls an Action<string>

public Action<string> DisplayError;

public void MyMethod()
{
  DisplayError("ERROR");
}

In this method I want to call DisplayError however I can see that if DisplayError is null it will thrown an exception.

I can run a test that proves that it will throw an exception.

So I know I want to add a if (DisplayError != null) to my code but I feel this design is wrong somehow. Maybe the test should be different?

  • 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-31T15:38:27+00:00Added an answer on May 31, 2026 at 3:38 pm

    Rather than making Action a public field, make it a property or pass it into the method. You can then do the null checking at that point. I also doubt that you need to retrieve it after setting it, so either:

    _displayError = (s) => { throw new ArgumentNullException("Please tell me what to do with this exception!"); };
    
    public Action<string> DisplayError
    {
        set 
        {
            if (value == null) { throw new ArgumentNullException("I need this!"); }
            _displayError = value;
        }
    }
    
    public void MyMethod()
    {
        _displayError("ERROR");
    }
    

    Or:

    public void MyMethod(Action<string> displayError)
    {
        if (displayError == null) { throw new ArgumentNullException("I need this!"); }
        displayError("ERROR");
    }
    

    The first of these shouldn’t require any code changes from your consumers.

    Of course, if you’re just working with a team who are using your code, you shouldn’t need to null-check at all. Just find out who’s using your code wrong and have a friendly chat to work out where the misunderstanding is. Defensive programming is not a good substitute for a team whose members trust each other, and null-checking is not as effective as having a clearly defined, easy-to-use interface.

    You’ll need 2 tests:

    My class:

    • should allow the consumer to handle any error
      • Given I’m going to produce an error for whatever reason (set context here)
      • When I produce an error (call the method, let the error handler set some variable)
      • Then I should give the error to the handler (check the variable that got set)
    • should make sure an error handler is attached
      • Given I might produce an error (you can code what you like here because it’s irrelevant)
      • When the consumer calls me without an error handler (call the method with a null error handler)
      • Then I should immediately ask the consumer not to do that (check for an exception)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that contains an asynchronous call like this: public void MyMethod()
I want my classes X and Y to have a method f(x) which calls
I have this code: s(x => x.Open()); s is a method which calls one
I have this code for login validation using a Struts2 action class which calls
I have a method which calls for a classmethod of another class def get_interface_params_by_mac(self,
I have a method which calls a method on an integer: def print_time(time =
I have a method invoking bean which calls a method to perform some sort
I have a method which constructs an object, calls an Execute method, and frees
The problem: I have a class which contains a template method execute which calls
I have a service method that calls a DAO which then returns an object

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.