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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:56:07+00:00 2026-06-03T04:56:07+00:00

I have a semi generic class that receives a Func<> from another class to

  • 0

I have a semi generic class that receives a Func<> from another class to tell it how to convert data. In a somewhat stripped down fashion, the code it looks like:

public class A
{
    public B b;

    public void init()
    {
        b = new B(ConvertRawData);
    }

    public double ConvertRawData(double rawData)
    {
        if(rawData == 1023)
            RaiseMalfunctionEvent();
        return rawData;
    }

    public void CalculateData(double rawData)
    {
        b.CalculateData(rawData);
    }
}

public class B
{
    event EventHandler Malfunction;

    Func<double, double> _converter;
    public B(Func<double, double> converter)
    {
        _converter = converter;
    }

    public void CalculateData(rawData)
    {
        var myVal = _converter(rawData);
    }
}

What I am wanting to do is be able to raise an event in the Func<> but be able to handle that event in class B. Right now the event is actually raised in class A.

So my question is how can I make class B without exposing a public event handler in class B? Or is this even possible?

Edit
As far as it being an event instead of an exception. This is external data from a sensor. The sensor sends 1023 to indicate it is in an error state, so I need to flag the data and notify the user, but would it really be an exception in my program?

  • 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-03T04:56:08+00:00Added an answer on June 3, 2026 at 4:56 am

    I agree with others, this looks like the best approach is to use exceptions: Create a new exception class ComnversionMalfunctionException, throw it in your delegate and then catch it in CalculateData(). In the catch clause, you can then raise the event.

    But if you don’t want to use exceptions, you can do this by passing a delegate to the converter delegate that raises the event:

    public class A
    {
        public double ConvertRawData(Action raiseMalfunctionEvent, double rawData)
        {
            if(rawData == 1023)
                raiseMalfunctionEvent();
            return rawData;
        }
    }
    
    public class B
    {
        event EventHandler Malfunction;
    
        Func<Action, double, double> _converter;
        public B(Func<Action, double, double> converter)
        {
            _converter = converter;
        }
    
        public void CalculateData(rawData)
        {
            var myVal = _converter(() => Malfunction(this, EventArgs.Empty), rawData);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
We have a semi-weird issue for our site. Consistently, from 1 anonymous* user the
I'm looking for information extraction libraries where I can have semi structured information that
I have a semi-working example that you can look at. This appears to work
Windows 7 windows naturally have a semi-transparent header. What I just realized is that
I have a BackgroundWorker that runs a lengthy and semi-complex task. In this BackgroundWorker,
I have a long list of Unicode values that are semi-colon delimited. Here's an
I have a semi-large web application that we run locally and I need to
I have a (semi)RESTful web service that I'm working on written in c++. We
I have a view with some UIButtons on it. I add another - semi

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.