Using dynamic properties in vb.net

Source: Internet
Author: User
Dynamic Visual Basic. NET supports dynamic properties, whose dynamic properties are stored in an application configuration file that will be read when the application executes. You can use dynamic properties to change some property values without recompiling the application. You can use them to save database connection strings, application log information, or server connection information.

To see the example of a dynamic property, first create a Windows application in Visual Basic. Add a textbox to the default form, make sure that the TextBox is selected, and then locate the dynamic property in the Properties window.

Click the ellipsis button next to the Advanced tab to open a dialog box that lists all the properties of the textbox, selects the Text property, and the key mapping box will be activated. The key mapping determines the value that the key stores in the configuration file. Click OK to close the dialog box.

The following code is generated in the InitializeComponent method (located in the Windows Form Designer generating code area). This code cannot be modified directly, only using the Properties window.

Dim Configurationappsettings As _
System.Configuration.AppSettingsReader = _
New System.Configuration.AppSettingsReader ()

Me.TextBox1.Text = CType (_
Configurationappsettings.getvalue ("TextBox1.Text", _
GetType (System.String)), String)

The settings are stored in an XML-formatted file named App.cofig. The preceding sample code produces the following XML code:

<?xml version= "1.0" encoding= "Windows-1252"?>
<configuration>
<appSettings>
<!--User application and configured property settings Go here.-->
<!--Example: <add key= "Settingname" value= "Settingvalue"/>-->
<add key= "TextBox1.Text" value= "TextBox1"/>
</appSettings>
</configuration>

When the application is compiled, the file is renamed to Assembly.config, where assembly is the name of your compiled application. If you want to change these values, there is no need to recompile the program, as long as a simple edit configuration file in which to modify the corresponding value.


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.