Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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#_Accessor - Fatal编程技术网

C# 控制多个类之间的复选框属性,这些类如何相互作用?

C# 控制多个类之间的复选框属性,这些类如何相互作用?,c#,accessor,C#,Accessor,我有两个班,一个是主程序,另一个是选项菜单。我对其进行了编码,以便options类能够在使用以下选项选中options菜单中的选项后立即显示或不显示复选框: 在主课堂上: //public boolean to manipulate the checkBox54 that exists on the main class public Boolean mostracaixabetaofresco { //the problem is this get, i d

我有两个班,一个是主程序,另一个是选项菜单。我对其进行了编码,以便options类能够在使用以下选项选中options菜单中的选项后立即显示或不显示复选框:

在主课堂上:

   //public boolean to manipulate the checkBox54 that exists on the main class
   public Boolean mostracaixabetaofresco
    {
        //the problem is this get, i don't understand what should i place in here, 
       //although i have read the msdn about accessors, it only has examples for variables, not properties

        get { return checkBox54.Visible; }
        set { checkBox54.Visible = value; }
    }

    //toolstrip button that redirects to the the options class
    private void configuraçãoToolStripMenuItem_Click(object sender, EventArgs e)
    {
          //formOpConfig is the name of the class with the options menu
        formOpConfig painelconfig = new formOpConfig(this);
        painelconfig.ShowDialog();
    }
在我的选项课上

public partial class formOpConfig : Form
{
    public static string xmlDialogconstante { get; set; }

    private Form1 Opener { get; set; }

    public formOpConfig(Form1 opener)
    {

        this.Opener = opener;
        this.Opener.mostracaixabetaofresco = false; //or false accordingly to needs
    }
 }
这很有效,但是我讨厌不知道我在做什么,我也不知道为什么我必须在get{}中放置checkBox54.Visible


我对编程非常陌生,几个月前在这方面帮助过我的人我无法联系上。有人能告诉我们这是如何工作的吗?

这里是对您的代码的基本回顾(我删除了不相关的代码):

1) 创建
formOpConfig
类的新实例:

formOpConfig painelconfig = new formOpConfig(this);
2) 这将导致构造函数执行,从而将第一个表单上的属性设置为
false

public formOpConfig(Form1 opener)
{
    ...
    this.Opener.mostracaixabetaofresco = false;
}
3) 调用“setter”并在第一个表单上隐藏您的
复选框

public Boolean mostracaixabetaofresco
{
    get { ... }
    set { checkBox54.Visible = value; }
}

你根本不需要有一个getter,但是如果你有一个getter,那么它必须返回一些东西

您可以完全删除它,因为您似乎不需要它:

public Boolean mostracaixabetaofresco
{
    set { checkBox54.Visible = value; }
}

你能给我指出重复的格兰特·温尼吗?我已经阅读了你提供的链接。我没有问访问器是如何工作的,因为我没有以一种通用的方式使用它们。我不认为它回答了我发布的问题。谢谢,也许我只是个傻瓜,但我已经阅读了示例并理解了它们,我只是不理解我的,或者我应该在get{}中放置什么,因为我知道set{}是让它在set中可见或不可见的东西,我理解我不需要放置任何其他东西,因为它会根据我的意愿将主窗体上的属性设置为true或false。但是如果我清空get{},它会提供一个错误,指出并非所有代码路径都返回一个值。那么get在这种情况下做什么呢?如果我清空它的内容,我会得到错误非常感谢,事实上我不知道在get{}中放置什么,但是是的,删除它解决了大部分疑问。如果我以某种方式惹恼了你,我很抱歉。声明:不要提供仅设置的属性或setter具有比getter更广泛的可访问性的属性。出于明显的原因这样做从来都不是一个好主意。我建议实现一个getter。@grantwiney省略setter是另一回事。这是实现只读属性的一般做法。那很好,但这里正好相反。显然,我的意思是:假设我们有一个
Rectangle
类,它公开了名为
Width
的“只写”属性,并且您将宽度设置为某个值。稍后,您想知道当前的
宽度是多少。您将如何实现这一点(作为一个只写属性)?这听起来很难看,不是吗?只读属性完全可以,但只读属性不能。