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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:52:59+00:00 2026-05-22T00:52:59+00:00

I have a situation where I have someFunction(int) , and I need to generate

  • 0

I have a situation where I have someFunction(int), and I need to generate programmatically n buttons that will call it. What this means is that I want to create buttons B1, B2, … Bn that call someFunction(1), someFunction(2), … someFunction(n) when clicked.

This is how I attempted to do this (semi-pseudocode):

for (int i = 1; i <= n; i++) {
  Button b = new Button();
  b.Caption = "Value " + n; // non-WPF: b.Text = "Value " + n;
  b.Click += (sender, event) => {
    someFunction(i);
  }
}

What bugs me about this is that when I click on the first button (B1), with a debugger over someFunction(i), it tells me that it’s calling someFunction(n + 1).

I’m not sure why this is, or how to fix it. The work-around I use is to use, instead of someFunction(i), someFunction(int.Parse(i.ToString()) (to create a copy of i). But this seems shady to me, because integers should be value types.

  • 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-22T00:53:00+00:00Added an answer on May 22, 2026 at 12:53 am

    I believe you understand WHY this happens. The problem is it captures the variable i itself, not its value. The workaround that seems better (without toString and int.parse) to me is to declare another local var that copies i

    for (int i = 1; i <= n; i++) {
    Button b = new Button();
      b.Caption = "Value " + n; // non-WPF: b.Text = "Value " + n;
      int locali = i;
      b.Click += (sender, event) => {
        someFunction(locali);
      }
    }
    

    This way the captured variable will be locali and it will remain the same across the loop.

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

Sidebar

Related Questions

I have a situation where I need to generate a table from a stored
I have situation where I need to make sure that user has completed certain
I have situation where I generate stylesheets based on information in the database. This
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation, where running a query that filters by an indexed column in
I have situation where I need to change the order of the columns/adding new
I have situation like this: user submits form with action='/pay' in '/pay' I have
Suppose we have a situation like this. Suppose instead of p = &global; we
I have situation where I have to call method of interface using reflection, like
I have situation. I have to create a Sports Club system in JAVA. There

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.