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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:50:52+00:00 2026-05-25T11:50:52+00:00

After hacking about for a little while on a prototype I’ve ended up with

  • 0

After hacking about for a little while on a prototype I’ve ended up with number of methods that update boolean flags on an object and then update interface and do some processing based on the new value. These are pretty much all the same – but the value they update is different

for example – imagine we have a bunch of colored boxes to update – I might have some methods that look like this:

        protected void SetBlueBoxVisibility(bool blueBoxVisibility)
    {
        Project project = LoadProject();
        project.ShowBlueBox = blueBoxVisibility
        ReDrawSomeThings();
        CalcualteSomeStuff();
        Project.UpdateBoxStatus();
        SaveProject(project);
        ShowBlueBoxPanel(blueBoxVisibility);
        RaiseStatusUpdated();
    }

    protected void SetRedBoxVisibility(bool redBoxVisibility)
    {
        Project project = LoadProject();
        project.ShowRedBox = redBoxVisibility
        ReDrawSomeThings();
        CalcualteSomeStuff();
        Project.UpdateBoxStatus();
        SaveProject(project);
        ShowRedBoxPanel(redBoxVisibility);
        RaiseStatusUpdated();

    }       

Now, obviously – most of that stuff is repeated – which is a pain when I come to change anything. Particularly if I end up with twenty different box colors rather than just two!

I was thinking that there must be a way to strip out the code that changes and gather the stuff that’s the same in a more generic method – but I’m having trouble getting to quite how to do that.

I’ve heard of closures – but I haven’t gotten my head around them enough to know if they would help here.

I was thinking possible the following might be on the right line – but I’m I don’t know how to tell the generic method which property to operate on – [Project Variable To Update]

        protected void SetRedBoxVisibility(bool redBoxVisibility)
    {
        SetGenericBoxVisibility([Project Variable To Update],redBoxVisibility)
        ShowRedBoxPanel(redBoxVisibility);
        RaiseStatusUpdated();   
    }

    protected void SetBlueBoxVisibility(bool blueBoxVisibility)
    {
        SetGenericBoxVisibility([Project Variable To Update],blueBoxVisibility)
        ShowBlueBoxPanel(blueBoxVisibility);
        RaiseStatusUpdated();   
    }

    protected void SetGenericBoxVisibility([Project Variable To Update], boxVisibility)
    {
        Project project = LoadProject();
        project.**[Project Variable To Update]** = boxVisibility
        ReDrawSomeThings();
        CalcualteSomeStuff();
        Project.UpdateBoxStatus();
        SaveProject(project);
    }

Any pointers as to how to handle this kind of thing would be useful 🙂

  • 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-25T11:50:52+00:00Added an answer on May 25, 2026 at 11:50 am

    Well, you could extract it like this:

    protected void SetGenericBoxVisibility(Action<Project> propertySetter,
                                           Action<bool> panelShower,
                                           bool boxVisibility)
    {
        Project project = LoadProject();
        propertySetter(project);
        ReDrawSomeThings();
        CalculateSomeStuff();
        Project.UpdateBoxStatus();
        SaveProject(project);
        panelShower();
        RaiseStatusUpdated();
    }
    

    Then:

    protected void SetBlueBoxVisibility(bool blueBoxVisibility)
    {
        SetGenericBoxVisibility(project => project.ShowBlueBox = blueBoxVisibility,
                                () => ShowBlueBoxPanel(blueBoxVisibility));
    }
    
    protected void SetRedBoxVisibility(bool redBoxVisibility)
    {
        SetGenericBoxVisibility(project => project.ShowRedBox = redBoxVisibility,
                                () => ShowRedBoxPanel(redBoxVisibility));
    }
    

    It’s not terribly nice, admittedly…

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

Sidebar

Related Questions

After surprisingly little hacking about, I managed to get libc++ installed on my Linux
I noticed today (after ~8 years of happily hacking away at bash) that there
While doing some hacking on my own site I encountered (after some googling) a
After reading the Head First Design Patterns book and using a number of other
After being told by at least 10 people on SO that version control was
After reading a bit more about how Gnutella and other P2P networks function, I
After hours of debugging, it appears to me that in FireFox, the innerHTML of
After a extensive debugging session I found that the problem was that I called
I'm 'hacking' my router, and I need to rewrite one JS function that takes
After debugging a CodeIgniter application that were installed into a new development environment, I

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.