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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:43:45+00:00 2026-05-22T23:43:45+00:00

I have code like this: public class SomeClass { private bool Flag; public void

  • 0

I have code like this:

public class SomeClass
{
     private bool Flag;

     public void OnBar() //this is called from DoSomething();
     {
        if (Flag) //For some reason Flag=false
     }

     public void OnFoo() //this is called from some anonymous method (not mine)
     {
        Flag = true;
        DoSomething();
     }
}

The Callstack looks like this:
AnonymousMethod();
OnFoo();
DoSomething();
OnBar();

I have read MSDN article about outer variables with anonymous methods but they apply to local variables, what about class level variables.

Why Flag is false inside OnBar() method, and how to solve this problem.

  • 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-22T23:43:46+00:00Added an answer on May 22, 2026 at 11:43 pm

    That definitively has nothing to do with anonymous methods. It must be something that happens inside DoSomething.
    There are basically two possibilities:

    1) Flag is re-set in DoSomething:

    private void DoSomething()
    {
        Flag = false;
        OnBar();
    }
    

    2) DoSomething creates a new instance of SomeClass and calls OnBar on that:

    private void DoSomething()
    {
        new SomeClass().OnBar();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like this: public class A { public void doStuff()
I have code like this: public class Thingy { private final Lock lock =
I have code that looks like this: template<class T> class list { public: class
Say, I have a code snippet like this: public static void main(String[] args) {
I have code that looks like this: public class Polynomial { List<Term> term =
i have code like this public class People { public string name { get;
I have a class like this: class SomeClass { public: QString data; SomeClass(const QString
I have some code like this: public class EffectValues : IEnumerable<object> { public object
I have simple code witch works ok: public class Example { private List<SomeClass> _list
If I have code like this: public XALServiceConfiguration CreateInstance() { var config = ConfigurationManager.GetSection(ConfigurationSectionName)

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.