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

  • Home
  • SEARCH
  • 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 7843917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:42:45+00:00 2026-06-02T16:42:45+00:00

I have a library with some methods, and during certain errors I want it

  • 0

I have a library with some methods, and during certain errors I want it to call a method as defined by the caller. This method must return a bool. What I mean is:

Sock sock = new Sock();
sock.OnSpecificError(theMethodToCall);

And then when the error happens, sock should first check to see if OnSpecificError has been set. If it has, it should run it. If the method returns false or if OnSepcificError hasn’t been set, it should throw an exception.

This seems like the sort of thing that delegates are designed for, but for whatever reason I can’t seem to get my mind around what those are actually doing, as in, what is being passed around and how I’d run it later. My current method of handling this is stupid and cumbersome, so any help would be greatly appreciated.

Edit:
Let me clarify a bit. sock.OnSpecificError(method) is used to simply pass sock a method, which it will store, but not run. The idea is then that later one, while sock is running some other methods, if the error pops up, it should run that method. I want to keep the method for use later, and then run it if needed.

  • 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-06-02T16:42:46+00:00Added an answer on June 2, 2026 at 4:42 pm

    The easiest way to start with delegates is by using pre-defined ones. For example, a delegate that takes no arguments and returns a bool would be Func<bool>. A delegate that takes a string and an int, and returns a bool would be Func<string,int,bool>, and so on.

    You define your method like this:

    void OnSpecificError(Func<bool> errorCheck) {
        // Do your stuff...
        var gotErrors = errorCheck();
        if (gotErrors) {
            // Do something about it
        }
    }
    

    You can now call OnSpecificError like this:

    sock.OnSpecificError(CheckForErrors);
    
    private bool CheckForErrors() {
        return errorCode.Equals("YES");
    }
    

    You can also use lambdas to inline small methods, like this:

    sock.OnSpecificError(() => errorCode.Equals("YES"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a C++ library where some methods are defined and exported and are
I'm building a library with a some methods and I have a method extend
Suppose, I have an opensource project that depends on some library, that must be
I have a failure in some inner library function in Octave. I want to
I have a graph library that I use to plot some data. This data
In a library I am writing, I have some methods which would only be
I have some PageMethods (static methods in a page marked with <WebMethod> ) defined
I have a class library with some extension methods written in C# and an
I have a small Utility library containing some useful utility methods which have been
I have a library that I created with some business logic that includes writing

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.