Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms Designer, Rationale behind Design > GenerateMember

In Visual Studio, while working on Windows Forms Applications a certain option can be found in the Form Design View. I'm talking about the GenerateMember option (under the Design section of the Properties Window).

The description of this option is the following:

Indicates if a member variable will be generated for this component.

So, if I create a text box tb1 using the Form Dessigner into my Form f1 and I set this option to false, then I cannot access the control within the code (neither f1.tb1 nor tb1 exists).

There should be a good reason for doing that, but I don't know what this reason could be, that's why I'm asking:

  • Whats the rationale behind the Design > GenerateMember option?
  • What things can be achieved setting this option to false that cannot be achieved with it on true?
  • In What situations I should preffer to hide the control setting GenerateMember to false instead of letting it to have the default value (true)?

Thank you.

like image 257
PaperBirdMaster Avatar asked Dec 04 '15 15:12

PaperBirdMaster


1 Answers

When GenerateMember is set to false, instead of having a lot of private fields scoped to the Form class instance, they are created scoped to the InitializeComponent() method - meaning you will no longer have this.btnOk or this.btnCancel accessible to anything within your form.

I've figured it is, for the most part, just a personal preference. By specifying which controls you want directly visible as private fields to the rest of your form class, you can avoid getting lost in a sea of unused fields when modifying your code.

Other than that, there is no real functional difference. You could still access the controls via: this.Controls collection.

like image 66
gmiley Avatar answered Nov 14 '22 18:11

gmiley