How to fix “inaccessible due to its protection lev

2020-04-16 04:04发布

I have a Form class

    partial class ProgressMainForm : Form
    {
      public ProgressMainForm()
      {
        InitializeComponent();
      }
    }

And then a class that uses that class and contains all functionality for the user

    public class ProgressForm
    {
        public ProgressMainForm myProgressForm;

        public ProgressForm(string title)
        {
            myProgressForm = new ProgressMainForm();
            myProgressForm.Text = title;
        }

        public void SetProgressBar(int min, int max)
        {
        ....
    }

I then use this ProgressForm class in my project like this

            progresswindow = new ProgressForm("Replacing All Strings");

This way progresswindow only contains members that are related to the functionality of the ProgressForm and all those Form members are hidden from the user.

But sometimes I need to access those Form members, for example when I need Invoke method.

Is there a way to make myProgressForm in ProgressForm accessible to users without making ProgressMainForm public?

Or is this approach wrong?

标签: c# .net winforms
4条回答
在下西门庆
2楼-- · 2020-04-16 04:13

In my opinion you should not work with the form directly. If I read your setup correctly, you want to show progress indicator while some job is being done. ProgressForm should expose methods to set the counters and increment them; as you run it on another thread, form manipulation should be done from inside the methods of ProgressForm. Your Invokes belong there, wrapped in suitable methods. If you want to change some visual properties of ProgressMainForm relay those properties to ProgressForm.

To resume, calling code should have no clue what ProgressForm does other than setting progress boundaries, starting, setting current percentage and stopping. This way, if you are asked to port the application to another UI system the amount of code you will need to change will be drastically reduced.

查看更多
家丑人穷心不美
3楼-- · 2020-04-16 04:16

Wrap or Expose the Methods you need. But somehow i don't like the approach, restricting the access is not a bad idea but should not be the whole purpose of this kind of abstraction. Try to make the acess easier, not restrictive.

查看更多
forever°为你锁心
4楼-- · 2020-04-16 04:18

You can declare the methods as internal , This will allow you to call the methods from within the assembly.

查看更多
姐就是有狂的资本
5楼-- · 2020-04-16 04:23

Is there a way to make myProgressForm in ProgressForm accessible to users without making ProgressMainForm public?

Yes, you can create some public properties on ProgressForm that expose specific properties of ProgressMainForm.

private ProgressMainForm myProgressForm;

public int SomeProperty
{
  get { return myProgressForm.IntProp; }
  set { myProgressForm.IntProp = value; }
}

For readonly properties, omit the set, and for any types that are reference types, you may want to return a clone or copy (to ensure the client can't change it).

查看更多
登录 后发表回答