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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:01:54+00:00 2026-05-16T11:01:54+00:00

I have a piece of code that filters a list using LINQ, creates a

  • 0

I have a piece of code that filters a list using LINQ, creates a list of instances of an anonymous type, and assigns an event handler to each instance:

// Select every linear expression and create a menu item from it
var items = from expr in expressionList.Expressions
            where expr.Type == ExpressionType.Linear
            let stdExpr = (StandardExpression)expr
            select new
            {
                Menu = new ToolStripMenuItem(stdExpr.Expression), // string
                stdExpr.Slot // int
            };

// Wire a Click event handler to each menu to set the tracked line
foreach (var item in items)
{
    item.Menu.Click += (s, e) => graph.SetTrackedLine(item.Slot);

    menuTrackLineWithMouse.DropDownItems.Add(item.Menu);
}

This works well in that the event handlers get wired and the menus get added correctly. The problem comes when the menu item is clicked, and the handler is fired. No matter what menu item fired the handler, only the last one is ever passed to SetTrackedLine.

An example is if I have two menus, “sin(x)”, with slot 0, and “cos(x)”, with slot 1, both Click events pass 1 to SetTrackedLine, no matter if “sin(x)” was clicked or “cos(x)” was.

My question is, why does this happen? Shouldn’t item.Slot refer to each separate instance of the anonymous type?

Thanks.

  • 1 1 Answer
  • 1 View
  • 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-16T11:01:55+00:00Added an answer on May 16, 2026 at 11:01 am

    You are closing over the loop variable. The problem specifically is here:

    (s, e) => graph.SetTrackedLine(item.Slot)
                                   ^^^^
    

    The value of item used will be the current value when the lambda expression is run, not the value it had when it was created. This is a “gotcha” of C# and a common error.

    Try this instead:

    foreach (var item in items)
    {
        var item2 = item;
        item2.Menu.Click += (s, e) => graph.SetTrackedLine(item2.Slot);
        menuTrackLineWithMouse.DropDownItems.Add(item2.Menu);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code that creates dynamic controls using 2 while loops.
Does cgi scripts have something like servlet filters. I.e. a piece of code that
I have a piece of code that generates a class, Id3v1Tag, using a file
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have a piece of code that load a very big image in memory.
I have a piece of code that takes several rows from a database and
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have a piece of code that calls a WCF service that is hosted
If have a piece of code that gets some data from a sql database

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.