这是否有效,我应该以更好的方式使用封装吗?

Is this efficient and should I be using encapsulation in a better way?

这是代码。请在 post.

底部查看我的问题
public partial class myClass : Other.Class
    {
        long check1parameter = CurrentSession.CurrentFile.ID;

        protected override void EnquiryLoaded(object sender, System.EventArgs e) 
        {
            disableFields();
        }
        private void disableFields() 
        {
            if (checkEverything()) {
                EnquiryForm.GetControl("Status").Enabled = true;
            }
        }

        public bool check1_method(long check1parameter) {
            bool Check1 = false;
            string stringToCheck = check1parameter.ToString();
            if (stringToCheck.Contains("something")) {
                    Check1 = true;
                }
            return Check1;
        }

        public bool checkEverything() {
            bool roleCheck = CurrentSession.CurrentUser.IsInRoles("RequiredRole");
            bool check1 = check1_method(check1parameter);
            bool checkEverything = false;
            if (roleCheck && check1) {
                checkEverything = true;
            } 
            return checkEverything;
        }
        //other methods
    }

代码是检查某人是否有角色,以及字符串是否包含一些信息,然后禁用一个字段。我从实际代码中对此进行了简化,以概述要点。虽然目的只是 运行 这些简单的检查和禁用字段,但我认为最好为这些任务创建单独的方法,以便以后可以扩展它们。

我确实收到一个对象引用错误,在该位置定义了 long check1parameter。它在 check1_method() 中并且工作正常,但我希望它被声明一次并在可能的情况下跨多个区域使用。

我还想将 parameters\variables 传递给 check1_method 而不是在其中声明它们。使 check1parameter 可用于此部分 class 中的所有方法的最佳方法是什么?它引用另一个以某种方式链接到 Other.Class 的 class。

我的主要问题是 - 如何使它尽可能高效,我是否应该在这里的任何地方使用 private 代替 public?我对 C# 还是很陌生,还没有完全弄清楚封装,所以请放轻松! :)

myClass 不需要声明为部分,除非您打算在不同的文件中继续实现它。

当使用简单的 if 语句时,它们可以被删除,例如你可以写:

public partial class myClass : Other.Class
    {
        long check1parameter = CurrentSession.CurrentFile.ID;

        protected override void EnquiryLoaded(object sender, System.EventArgs e) 
        {
            disableFields();
        }
        private void disableFields() 
        {
            EnquiryForm.GetControl("Status").Enabled = checkEverything();
        }

        public bool check1_method(long check1parameter) {
            return check1parameter.ToString().Contains("something");
        }

        public bool checkEverything() {
            bool roleCheck = CurrentSession.CurrentUser.IsInRoles("RequiredRole");
            bool check1 = check1_method(check1parameter);

            return (roleCheck && check1);
        }
        //other methods
    }

为了避免声明不必要的布尔值。除此之外,您将牺牲可读性以换取更少的行数。

当谈到 public 与私有时,最好始终指定私有,除非您需要从 class 外部访问它。乍一看,disableFields() 应该是 public,而 check1_method()checkEverything() 是私有的。

编辑: 另外,如果 check1parameter 全局实例化为 myClass,则不需要将其作为参数传递给 check1_methods()

您提供的代码看起来没问题。我做了一些更改,主要是代码美学。主要的是将2个检查方法制作成属性。

public partial class myClass : Other.Class
{
    long check1parameter = CurrentSession.CurrentFile.ID;

    protected override void EnquiryLoaded(object sender, System.EventArgs e)
    {
        disableFields();
    }

    private void disableFields()
    {
        if (checkEverything)
        {
            EnquiryForm.GetControl("Status").Enabled = true;
        }
    }

    // the parameter name was the same as a variable in the class
    // renamed to avoid confusion
    public bool check1_method
    {
        get {return check1parameter.ToString().Contains("something");}
    }

    public bool checkEverything
    {
        get { return CurrentSession.CurrentUser.IsInRoles("RequiredRole") 
            && check1_method; }
    }
    //other methods
}