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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:36:21+00:00 2026-05-25T19:36:21+00:00

Consider the following control (snipped for brevity): public partial class ConfigurationManagerControl : UserControl {

  • 0

Consider the following control (snipped for brevity):

public partial class ConfigurationManagerControl : UserControl
{

    public Func<string, bool> CanEdit { get; set;}
    public Func<string, bool> CanDelete { get; set; }

    public Dictionary<string, string> Settings
    {
        get { return InnerSettings; }
        set
        {
            InnerSettings = value;
            BindData();
        }
    }
    private Dictionary<string, string> InnerSettings;

    private void OnListIndexChanged(object sender, EventArgs e)
    {
        this.EditButton.Enabled = false;
        this.DeleteButton.Enabled = false;

        var indices = this.List.SelectedIndices;
        if (indices.Count != 1)
        {
            return;
        }

        var index = indices[0];
        var item = this.List.Items[index];

        if (this.CanEdit != null)
        {
            this.EditButton.Enabled = this.CanEdit(item.Text);
        }

        if (this.CanDelete != null)
        {
            this.DeleteButton.Enabled = this.CanDelete(item.Text);
        }

    }
}

There’s more to this control, but suffice it to say that it allows a user to add, edit, and delete the entries in a Dictionary<string, string>. In order to determine whether or not it should allow the user to edit or delete the entries, it uses the delegate method properties, CanDelete and CanEdit, which are provided by the form or control that hosts it:

public class SetupWizard : Form
{
    public SetupWizard()
    {
        InitializeComponent();

        this.SettingManager.CanEdit = CanEditSetting;
        this.SettingManager.CanDelete = CanDeleteSetting;
    }

    private static bool CanEditSetting(string item)
    {
        var lockedSettings = new[] { "LicenseHash", "ProductHash" };
        return !lockedSettings.Contains(item.ToLower());
    }

    private static bool CanDeleteSetting(string item)
    {
        var lockedSettings = new[] {
                                        "LicenseHash",
                                        "ProductHash", 
                                        "UserName", 
                                        "CompanyName"
                                    };
        return !lockedSettings.Contains(item.ToLower());
    }
}

I find that this design is both satisfactory and worrisome at the same time. On the one hand, it seems to solve the problem using the simplest solution that works (it certainly separates the concerns nicely). On the other hand, I have this nagging concern that I am using delegates improperly and should be using an event, instead (even though I do not need multiple listeners, and only need the caller to tell me if the item is editable).

And then, on the other other hand, there’s the chance that there’s a completely different design that I haven’t even considered that might solve the problem in a vastly superior way.

So. Is this design technically correct, maintainable, and flexible? Or should I be doing something better?

  • 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-25T19:36:21+00:00Added an answer on May 25, 2026 at 7:36 pm

    I suggest the use of an interface with these two methods. That’s a lot cleaner:

    interface ICantThinkOfAGoodName
    {
        bool CanEdit(string item);
        bool CanDelete(string item);
    }
    

    You could create something similar to the RelayCommand used in many MVVM frameworks:

    public class RelayObject : ICantThinkOfAGoodName
    {
        public RelayObject() : this(null, null) {}
        public RelayObject(Func<string, bool> canEdit, Func<string, bool> canDelete)
        {
            if(canEdit == null) canEdit = s => true;
            if(canDelete == null) canDelete = s => true;
    
            _canEdit = canEdit;
            _canDelete = canDelete;
        }
    
        public bool CanEdit(string item)
        {
            return _canEdit(item);
        }
        public bool CanDelete(string item)
        {
            return _canDelete(item);
        }
    }
    

    Use it like this:

    public SetupWizard()
    {
        InitializeComponent();
    
        this.SettingManager.PropertyName = new RelayObject(CanEditSetting, 
                                                           CanDeleteSetting);
        // or (all can be deleted)
        this.SettingManager.PropertyName = new RelayObject(CanEditSetting, null);
        // or (all can be edited)
        this.SettingManager.PropertyName = new RelayObject(null, CanDeleteSetting);
        // or (all can be edited and deleted)
        this.SettingManager.PropertyName = new RelayObject();
    
    }
    

    BTW: I am using Property injection here, because it is a control. Normally, I would pass the ICantThinkOfAGoodName dependency in the constructor of the ConfigurationManagerControl.

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

Sidebar

Related Questions

Consider following code public class City { public string Name { get { return
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following code snippet: class Test { public int Length{ get; set; }
consider the following class and struct public class Entity { public IdType Id {get;set;}
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following method signatures: public fooMethod (Foo[] foos) { /*...*/ } and public
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following Control/Template <my:ExpandingListBox Margin=0,2,0,0 x:Name=TagSearchListBox> <my:ExpandingListBox.Template> <ControlTemplate TargetType={x:Type my:ExpandingListBox}> <Border Name=MyBorder BorderThickness=2
Consider the following public method that adds an integer variable to a vector of
Consider the following string: ABC . I would like to capture the following groups

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.