0

I have two classes, one is the main program, the other a options menu. I coded it so that the options class is able to get a check box visible or invisible as soon as the option in options menu is checked using the following:

on the main class:

   //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();
    }

on the options class i have

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
    }
 }

This works pretty well, but i hate not knowing what i'm doing, I'm also not sure why do i have to place checkBox54.Visible in the get {}.

I'm very new to programming, and i'm not able to contact the person who helped me on this a few months ago. Can someone bring some lights into how this is working?

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
ng80092a
  • 77
  • 1
  • 1
  • 9
  • can you point me to the duplicate grant winney? – ng80092a Aug 23 '14 at 16:06
  • I had already took a read on the link you provided. I didn't ask how accessors work because i'm not using them in a generalistic way. I don't think it answers the question i posted. – ng80092a Aug 23 '14 at 16:13
  • thank you, maybe i'm just dumb, but i've read the examples and understood them, i just don't understand mine, or what should i place inside the get { }, because, i'm aware that the set {} is what makes it visible or not – ng80092a Aug 23 '14 at 16:25
  • inside the set i understand i don't have to place anything else, because it will set the property on the main form true or false as i wish. but if i empty the get {} it provides an error saying that not all code path returns a value – ng80092a Aug 23 '14 at 16:30

1 Answers1

0

Here's a basic review of your code (I removed unrelated code):

1) You create a new instance of your formOpConfig class:

formOpConfig painelconfig = new formOpConfig(this);

2) That causes your constructor to execute, which sets the property on the first Form to false:

public formOpConfig(Form1 opener)
{
    ...
    this.Opener.mostracaixabetaofresco = false;
}

3) Which calls the "setter" and hides your CheckBox on the first Form:

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

You don't have to have a getter at all, but if you do have one, then it has to return something.

You can completely remove it, since you don't seem to need it:

public Boolean mostracaixabetaofresco
{
    set { checkBox54.Visible = value; }
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • So what does the get do in this case? if i empty its contents i get errors – ng80092a Aug 23 '14 at 16:27
  • Thank you very much, indeed i wasn't understanding what to place in the get {}, but yes, removing it solved most of the doubt. I'm sorry if i annoyed you in some way. – ng80092a Aug 23 '14 at 16:43
  • [Framework design guidelines](http://msdn.microsoft.com/en-us/library/ms229006%28v=vs.110%29.aspx) states that : *DO NOT provide set-only properties or properties with the setter having broader accessibility than the getter.* It is never a good idea to do so for obvious reasons. I suggest implementing a getter. – Sriram Sakthivel Aug 23 '14 at 16:49
  • @GrantWinney Omitting the setter is different story. That is general practice to implement read only properties. That is fine, but here it is other way around. By obvious reason I meant: Assume we have a `Rectangle` class which exposes "write only" property named `Width` and you set the width to something. Later at some point in time you wanted to know what is the current `Width`. How'll you achieve that(being it a write only property)? That sounds a ugly design isn't it? Read only properties are completely fine but write only properties are not. – Sriram Sakthivel Aug 23 '14 at 17:12