Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use automatic execution attributes in Visual Studio 2010

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "Visual Studio 2010 automatic execution attribute how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Visual Studio 2010 automatic execution attribute how to use" this article.

Automatic properties allow you to quickly specify the properties of a class without having to write code for the "Get" and "Set" attributes. When you write code for an automatically executed property, the Visual Basic compiler automatically creates a private field to store the property's variable, as well as the associated "Get" and "Set" steps.

With automatic properties, a property (including a default value) can be declared in a single line. The following example shows the declaration of three properties.

Public Property Name As String Public Property Owner As String = "DefaultName" Public Property Items As New List (Of String) From {"M", "T", "W"} Public Property ID As New Guid ()

An automatically executed property is equivalent to a property whose value is stored in a private field. The following code example shows an auto-execution property.

Property Prop2 As String = "Empty"

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

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

Supported field

When you declare an auto-execute property, Visual Basic automatically creates a hidden private field called "support field" to hold the value of the property. This supported field name is the name of an auto-executed property preceded by an underscore (_). For example, if you declare an automatically executed attribute named ID, the supporting field is named _ ID. If you include a number of your class whose name is also _ ID, you create a naming conflict and Visual Basic will report a compiler error.

This support field also has the following features:

◆ access to this supporting field modifier is always private, even when the property itself has a different access level, such as public.

◆ if the attribute is marked as shared, this supported field is also shared.

The property specified by ◆ for this property does not apply to this supported field.

Supporting properties can be accessed from code in this class and from debugging tools such as Watch window. However, this support field does not appear in the IntelliSense word completion list.

Initialize an automatic execution property

Any expression that can be used to initialize a field is legal to initialize an automatic property. When you initialize an auto-execute property, the expression is evaluated and passed to the Set step of the property. The following code example shows some automatic properties that include initial values.

Property FirstName As String = "James" Property PartNo As Integer = 44302 Property Orders As New List (Of Order)

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

When you declare an auto-execution property as a group number, you can specify a clear group number boundary. 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}

Attribute definitions that require standard syntax

Automatically executed properties are convenient and support many programming situations. However, there are situations where you cannot use auto-executed properties and must use standard or extended attribute 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 an attribute, such as code that validates the input value 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 required number of digits.

◆ specifies different accesses for the Get and Set steps. For example, you might want the Set step to be private and the Get step to be public.

◆ creates 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.

◆ adds an attribute to the support field.

◆ provides XML comments for the support fields.

Extend an automatically executed property

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

The above is all the content of the article "how to use automatic execution attributes in Visual Studio 2010". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report