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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:47:28+00:00 2026-05-17T15:47:28+00:00

In my current situation I have a class that performs an operation synchronously. During

  • 0

In my current situation I have a class that performs an operation synchronously. During that operation an certain event will the thrown several times, depending on how often the situation where the event is raised occurs.
I know how the event-mechanism works and what the recommended ways of using are. Works well.(Note: My application is single-threaded)

What I want to is, to set a value to a property in my EventArgs. I’ve never had the need to set properties there.

This is a simplified situation of my current situation: (Note, I don’t need answers telling me to use regex, or stringreplace, because that won’t work in this situation)

I have this EventArgs:

public class TestEventArgs : EventArgs
{

    public String OldString { get; private set; }
    public String NewString { get; set; }

    public TestEventArgs(String oldString)
    {
        this.OldString = oldString;
    }
}

I do normally raise events this way:

public event EventHandler<TestEventArgs> ChangeString;

protected virtual void OnChangeString(String oldString)
{
    EventHandler<TestEventArgs> handler = this.ChangeString;
    if (handler != null)
    {
        handler(this, new TestEventArgs(oldString));
    }
}

and by calling the OnChangeString method, because I read that this in the way to raise events a long time ago.

Modified code where I need a value of the EventArgs after it’s been raised:

public event EventHandler<TestEventArgs> ChangeString;

protected virtual void OnChangeString(TestEventArgs args)
{
    EventHandler<TestEventArgs> handler = this.ChangeString;
    if (handler != null)
    {
        handler(this, args);
    }
}
    public void Foo()
    {
        String oldString = "this is the old string";
        // this.OnChangeString(oldString) // this is the way I called before
        // now I need to keep a reference to the EventArgs
        TestEventArgs args = new TestEventArgs(oldString);
        this.OnChangeString(args);
        // here I do have full access to args.NewString
    }

So, is it ok to keep a reference for the EventArgs and to raise the event with a method that accepts my EventArgs as parameter?

  • 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-17T15:47:29+00:00Added an answer on May 17, 2026 at 3:47 pm

    I think your question boils down to:

    Is it OK to use the the arguments of an event to communicate data back to the source of the event?

    First and foremost: at the low level, this will work. You can modify the event object, and you can see those changes in the code that raises the event.

    But what if there are multiple event listeners. Who gets to set NewString?

    If you expect only one, it is more clear to pass a delegate (of type Func<String, String>) to the class that now raises the event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly standard inheritance situation in my current LINQ-to-SQL project. I have
Current situation: I have the current version of my MVC Framework which uses classes
I have a class (NamedPipeManager) which has a thread (PipeThread) that waits for a
In a current (C#) project we have a 3rd party assembly that contains a
The situation: I need to convert our current development environment from Windows XP 32-bit to
Current, I've got a stored procedure that has a main goal of doing a
Here is my situation. I have written a WCF service which calls into one
I have a query that selects data into a custom type- UserData curData =
Here is my situation: I have a solution with three projects: 1) Silverlight App,
The idea is to create a class that exposes a context but handles the

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.