Visual Studio 2010 automatically performs properties

Source: Internet
Author: User
Tags expression visual studio visual studio 2010

Detailed Automatic property
in Visual Studio 2010




in Visual Studio 2010, there are a number of features to help you better and faster completion of development work. This article will describe the automatic execution properties.





automatically executes properties that allow you to quickly specify the properties of a class without having to write code for the property "get" and "Set". When you write code for an automatically executed property, the Visual basic compiler automatically creates a private field that stores the variable for this property, and also creates related "get" and "Set" steps.





an automatically executed property, a property, including a default value, can be declared in a single row. The following example shows the declaration of three properties.





Public Property Name as String


Public Property Owner as String = "DefaultName"


Public Property Items as the New List (of String) from {"M", "T", "W"}


Public Property ID as New Guid ()





an automatically executed property is equivalent to a property in which a property value is stored in a private field. The following code example shows an automatic execution property.





Property Prop2 as String = "Empty"





The following code example shows the equivalent code that previously performed an attribute example automatically.





Private _prop2 as String = "Empty"


Property Prop2 as String


Get


return _PROP2


End Get


Set (ByVal value as String)


_PROP2 = value


End Set


End Property





Support Fields





when you declare an automatic execution of a property, Visual basic automatically creates a hidden private field called "Supported Fields" to hold the value of this property. This support field name is an automatically executed property name preceded by an underscore (_). For example, if you declare an automatically executed ID for a property name, the Support field is named _id. If you include a number of your class with a name that is also _id, you create a naming conflict, and Visual Basic reports a compiler error.





This support field also has the following features:





* Access to this support field modifier is always private, even when the attribute itself has a different level of visit (such as public).


* If the attribute is marked as shared, this support field is also shared.


* The attribute specified for this attribute does not apply to this support field.





support properties can be accessed from code access in this class and from debugging tools such as Watch window. However, this support field is not displayed in the IntelliSense (IntelliSense) Word completion list.





Initializes an automatic execution property




Any expression that
can use to initialize a field is valid for initializing an automatic execution property. When you initialize an automatic execution property, the expression is evaluated and transmitted to the set step of the property. The following code example shows some of the automatically executed properties that include the initial values.





Property FirstName as String = "James"


Property PartNo as Integer = 44302


Property Orders as New List (in order) (500)





You cannot declare an automatically executed property as a member of a structure (struct). If it is marked as shared, you can initialize this automatic execution property.





when you declare an automatic execution attribute as a group number, you can specify an explicit number of group boundaries. However, you can use a group initialization program to provide a value, as shown in the following example:


Property Grades as Integer () = {90, 73}


property temperatures as Integer () = New Integer () {68, 54, 71} requires a standard syntax attribute definition





properties that are automatically executed are convenient and support a number of programming scenarios. However, there are some situations where you cannot use an automatically executed property, and you must use a standard or extended property syntax.





If you want to do one of the following, you must use the attribute definition syntax:





* Adds code to the get or set step of a property, such as adding code to validate input values in the set step. For example, you might validate a string before setting this property value. This string displays a phone number that contains the number of digits required.


* Specifies different accessibility for get and set steps. For example, you might want the set step to be private and let the get step be public.


* Create write-only or read-only properties


* uses parameterized properties (including default properties). You must declare an extended familiarity to specify a parameter for this property or an additional parameter for the set step.


* Add a property to the support field.


* provides XML annotations for the supported fields.





extends an automatically executed property





If you have to convert an automatically executed property to an extended property that contains a get or set step, the Visual Basic code Editor can automatically generate the get and set steps and the End property declaration for this property. If you place the cursor in a blank line after the attribute declaration, enter a G (for get) or an S (for set) and press ENTER, and the code is generated. When you press the ENTER key at the end of the property declaration, the Visual Basic code Editor automatically generates the read-only and write-only properties of the get or set step.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.