Click here to Skip to main content
13,004,188 members (76,172 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 24 Jul 2013

Example: Getting Data Back from an EventHandler

, 18 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Getting data back from an EventHandler.


I previously wrote about why I like to use events here and here. I figured it would be appropriate to illustrate a simple case where you can delegate decisions between functionally separate parts of code (say, between an application layer and a presentation layer). If you're well versed in C# and .NET, this might put you to sleep. If you have no idea what I'm talking about, hopefully this will help.

The Scenario

Let's assume we have a layered application, which is usually my go to. I might have three layers: one for data persistence, one for my business logic and one for interacting with the user. I'll call these the data, application, and presentation layers, respectively. Let's also assume that the dependency flow works from the top layer down (so presentation layer depends on application and data, and application depends on data). Nice.

In my application layer, I have a class that can process data. Specifically, it can capitalize an input string and spit out the capitalized output. However, some input characters are not letters A-Z or a-z... So how should our processor behave? Let's have it delegate this decision to the user (who interacts with the presentation layer) and get back our result.

The Workflow

The following is the workflow for the application:

  • Create a processor object
  • Hook on the event handler so that the presentation layer can handle the corresponding event
  • Start processing the input
    • Capitalize valid characters
    • When we reach an invalid character...
      • Call our data processor's event to delegate the work to anyone who can make the decision for us (in this case, our presentation layer)
      • Have the event handler (the presentation layer) ask the user if we should skip the input
      • Store the result back on the event args
      • Back in the processor class, based on the result of the event args, we can skip or add the character.


It's a pretty straight forward task really. Because event args are objects in C#, we can store state on them and pass them around easily. If they were value types, we'd have to rely on function calls to return the user's decision as to whether or not we should skip the input. In my opinion, this is a bit uglier (but certainly still doable).

I've created a sample project and hosted it at Google Code, here. Check it out!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nick Cosentino
Team Leader Magnet Forensics
Canada Canada
I graduated from the University of Waterloo for Computer Engineering and I'm fortunate enough to work as a Team Lead of Software Engineering at Magnet Forensics. As a team lead, I'm often looking to encourage better coding standards, creative approaches to problem solving, and ensure that good clean code makes it into the code base. I want my team to produce top-notch code, but I want to make sure that we're all learning to become better developers along the way.


You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 18 Sep 2013
Article Copyright 2013 by Nick Cosentino
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid