Training
Module
Control variable scope and logic using code blocks in C# - Training
Use code blocks with more confidence, understanding how they impact the visibility and accessibility of both higher and lower-level constructs in your code.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
When you place a component on a Windows Form, two properties are provided by the design environment: GenerateMember
and Modifiers
. The GenerateMember
property specifies when the Windows Forms Designer generates a member variable for a component. The Modifiers
property is the access modifier assigned to that member variable. If the value of the GenerateMember
property is false
, the value of the Modifiers
property has no effect.
In Visual Studio, in the Windows Forms Designer, open your form.
Open the Toolbox, and on the form, place three Button controls.
Set the GenerateMember
and Modifiers
properties for each Button control according to the following table.
Button name | GenerateMember value | Modifiers value |
---|---|---|
button1 |
true |
private |
button2 |
true |
protected |
button3 |
false |
No change |
Build the solution.
In Solution Explorer, click the Show All Files button.
Open the Form1 node, and in the Code Editor,open the Form1.Designer.vb or Form1.Designer.cs file. This file contains the code emitted by the Windows Forms Designer.
Find the declarations for the three buttons. The following code example shows the differences specified by the GenerateMember
and Modifiers
properties.
private void InitializeComponent()
{
// button3 is declared in a local scope, because
// its GenerateMember property is false.
System.Windows.Forms.Button button3;
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
button3 = new System.Windows.Forms.Button();
Private Sub InitializeComponent()
' button3 is declared in a local scope, because
' its GenerateMember property is false.
Dim button3 As System.Windows.Forms.Button
Me.button1 = New System.Windows.Forms.Button()
Me.button2 = New System.Windows.Forms.Button()
button3 = New System.Windows.Forms.Button()
// The Modifiers property for button1 is "private".
private Button button1;
// The Modifiers property for button2 is "protected".
protected Button button2;
// button3 is not a member, because
// its GenerateMember property is false.
' The Modifiers property for button1 is "Private".
Private button1 As Button
' The Modifiers property for button2 is "Protected".
Protected button2 As Button
' button3 is not a member, because
' its GenerateMember property is false.
Note
By default, the Windows Forms Designer assigns the private
(Friend
in Visual Basic) modifier to container controls like Panel. If your base UserControl or Form has a container control, it will not accept new children in inherited controls and forms. The solution is to change the modifier of the base container control to protected
or public
.
.NET Desktop feedback feedback
.NET Desktop feedback is an open source project. Select a link to provide feedback:
Training
Module
Control variable scope and logic using code blocks in C# - Training
Use code blocks with more confidence, understanding how they impact the visibility and accessibility of both higher and lower-level constructs in your code.