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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:33:45+00:00 2026-06-11T02:33:45+00:00

I have this method: public override void respond(params string[] resargs) { foreach (command cmd

  • 0

I have this method:

public override void respond(params string[] resargs)
{
    foreach (command cmd in pndgcmds)
    {
        cmd(this);
    }

    if (objs[resargs[0]].status)
        objs[resargs[0]].request(resargs);
    else
    {
        pndgcmds.Add(
            (myclass master) =>
            {
                if (master.objs[resargs[0]].status != true) return;
                master.objs[resargs[0]].request(resargs);
                //code to remove the delegate
            });
    }
}

which checks if an object is able to respond, makes it respond if it does, otherwise stores the command in a dictionary so that the status is checked and called the next time a respond is called.

I have two questions.

  1. How do I write code to remove the delegate? I have no idea how to do that.

  2. As you can see, much of the inner workings of the delegate relies on method parameters. How will the delegate behave?

Thanks in advance!

  • 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-11T02:33:47+00:00Added an answer on June 11, 2026 at 2:33 am

    Anonymous methods are a pain to remove, as the instance of the captire-context is only available when creating it, an is required. Fortunately, there’s a trick:

    SomeDelegateType handler = null;
    handler = (myclass master) =>
                {
                    if (master.objs[resargs[0]].status != true) return;
                    master.objs[resargs[0]].request(resargs);
                    pndgcmds.Remove(handler);
                };
    pndgcmds.Add(handler);
    

    This is now a self-unsubscribing handler, that will unsubscribe itself when invoked. Note that to do this we need to store the delegate instance (handler), but we then create a closure over that variable. The initial =null is a requirement of definite assignment.

    You should note that the resargs variable is also captured, and this could lead to confusion if the value at index-zero changes after subscription but before it is invoked. If you want to use the value “now”, then close over that instead:

    SomeDelegateType handler = null;
    string val = resargs[0];
    handler = (myclass master) =>
                {
                    if (master.objs[val].status != true) return;
                    master.objs[val].request(resargs);
                    pndgcmds.Remove(handler);
                };
    pndgcmds.Add(handler);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method: public void testJSNI2(){ String x = test; } I can
I have this code in my custom MembershipProvider: public override void Initialize(string name, System.Collections.Specialized.NameValueCollection
In code-behind of an ASP.NET page I have this method: public string TestFunc() {
I have this method call: public DataTemplate Create(Type type, string propertyName) { string str
I have this method in cs page: public String getToolTip(Object productId, Object imgBtnId) {
I have this method to display the contact numbers in my inbox: public ArrayList<String>
I have a thread with this run method: public void run(){ MAPTable t1 =
Well lets say we have this c# code: public override void Write(XDRDestination destination) {
So I have this code: converteeUnit.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View arg1,
I have this code: button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //

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.