Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 这是否有效?我是否应该以更好的方式使用封装?_C#_Encapsulation - Fatal编程技术网

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

C# 这是否有效?我是否应该以更好的方式使用封装?,c#,encapsulation,C#,Encapsulation,这是代码。请看我在这篇文章底部的问题 public partial class myClass : Other.Class { long check1parameter = CurrentSession.CurrentFile.ID; protected override void EnquiryLoaded(object sender, System.EventArgs e) { disableFields()

这是代码。请看我在这篇文章底部的问题

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()
中,工作正常,但我希望声明一次,并尽可能跨多个区域使用

我还想将参数\变量传递给
check1\u方法
,而不是在其中声明它们。使
check1parameter
可用于此部分类中的所有方法的最佳方法是什么?它指的是以某种方式链接到
Other.class
的另一个类


我的主要问题是-我如何尽可能提高效率,我应该在这里的任何地方使用
private
而不是
public
?我对C还是很陌生,还没有完全理解封装,所以请对我放松点!:)

myClass
不需要声明为partial,除非您打算继续在其他文件中实现它

使用简单的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和private时,最好总是指定private,除非您需要从类外访问它。乍一看,
disableFields()
应该是公共的,
check1\u method()
checkEverything()
应该是私有的

编辑:
另外,如果将
check1parameter
全局实例化为
myClass
,则不需要将其作为参数传递给
check1\u方法()
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和private时,最好总是指定private,除非您需要从类外访问它。乍一看,
disableFields()
应该是公共的,
check1\u method()
checkEverything()
应该是私有的

编辑:
另外,如果将
check1parameter
全局实例化为
myClass
,则无需将其作为参数传递给
check1\u methods()

您提供的代码看起来正常。我做了一些更改,主要是代码美学。主要的一个是将这两个检查方法转换为属性

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
}

您提供的代码看起来不错。我做了一些更改,主要是代码美学。主要的一个是将这两个检查方法转换为属性

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
}

check1parameter
是一个长参数,为什么要将它与字符串
“something”
进行比较?虽然它是一个长参数,但偶尔会包含文本并转换为字符串。我正在检查字符串中是否有“something”(注意,它被转换为一个位于-
stringToCheck=check1parameter.ToString()上方的字符串。ToString()是一个数字类型,因此它只能包含一个整数,您是指使用
char[]
还是
byte[]
?还是我很困惑,你不是在比较字符串文字
“Something”
,而是指与
“123456”类似的东西
某些东西可以用任何文本/数字替换。我不是在比较文字,只是检查是否存在类似
abc123
的东西。这就是我要说的,
check1parameter
永远不能包含
“abc123”
。如果您尝试将sring分配给
check1parameter
,它将抛出一个错误。如果您希望它灵活,也许您应该将其声明为
var
对象
check1parameter
很长,为什么要将其与字符串
进行比较
?虽然它是一个长字符串,但它偶尔会包含文本并转换为字符串。我正在检查字符串中是否包含“某物”(注意,它被转换为一个位于-
stringToCheck=check1parameter.ToString()上方的字符串)
长字符串是一个数字类型,因此它只能包含一个整数,您的意思是使用
字符吗[]
字节[]
?或者我是否感到困惑,您实际上不是在比较字符串文字
“Something”
,而是指与
“123456”类似的内容
某些东西可以用任何文本/数字替换。我不是在比较文字,只是检查是否存在类似
abc123
的东西。这就是我要说的,
check1parameter
永远不能包含
“abc123”
。如果您尝试将sring分配给
check1parameter
,它将抛出错误。如果您希望它灵活,可能应该将其声明为
var
object
?删除disableField中的If语句在逻辑上并不等效。每次调用disable字段时,您的代码都会更新状态。original代码只有在checkEverything为true时才会启用该字段。@PaulTsai true,只是函数disableFields()意味着该字段可能被禁用。如果@jbab8打算使用封装,并且仍然将
inquiryform.GetControl(“Status”).Enabled
设置为false,则在其他地方为def