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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:40:09+00:00 2026-05-15T17:40:09+00:00

I have a fairly generic class (Record) that I have added a callback handler

  • 0

I have a fairly generic class (Record) that I have added a callback handler to, so I can do something like the following.

record.Save(AfterSaveMethod);

Which also returns the identity number of the record created. The issue I have now is that I have this nested save routine in a loop, and I need to use/pass the i variable!

for (int i; i < count ;i++)
{
    record.Save(AfterSaveMethod2) //but I need to pass i through as well
}  

What do I do here?

A\ rewrite the save method and include it in this class (yuk),

B\ have an overloaded save option that takes an object as a parameter, so I can pass it through to my record class, and then merge it with the identity number. Returning both the identity number and anything extra contained in the passed through object (hmm, sounds a bit messy),

C\ is there a sexier option?

  • 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-15T17:40:09+00:00Added an answer on May 15, 2026 at 5:40 pm

    This is where anonymous methods or lambda expressions are really handy 🙂

    Try this (assuming C# 3):

    for (int i = 0; i < count; i++)
    {
        int index = i;
        record.Save(id => AfterSaveMethod2(id, index));
    }
    

    Note that the lambda expression here is capturing index, not i – that’s to avoid the problems inherent in closing over the loop variable.

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

Sidebar

Related Questions

I have the feeling that i'm missing something key here. I've tried following guides
I have a fairly generic model and collection of that model (see below) I
Let's say I have a fairly generic jQuery Prompt that asks me to rename
I have a fairly simple web2py form with a few validators like, IS_FLOAT_IN_RANGE(...) on
I have a fairly straightforward Java class here which creates 2 thread pools.... Connects
I have a fairly standard OLTP normalised database and I have realised that I
This is a fairly generic question in that I'm just wondering what potential options
I have a fairly simple model that uses Django Taggit for tagging. Everything works
I have read around that serializing generic classes is not supported out of the
I have a class that I need to binary serialize. The class contains one

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.