TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Const Values

Use the Const keyword on Strings and Integers to indicate constant values.
Const. A Const variable cannot be changed. It is immutable. In the VB.NET language, Strings can be decorated with the Const keyword. Value types such as Integer may also be made constant. This influences performance.Integer

Tip: Const values resolve faster than regular variables. They should be used when possible.

Example. This program uses Const values in several ways. It uses a Const at the level of the Module: the syntax here is similar to a field. It also uses a Const at the level of the method: the syntax resembles a local variable Dim.

And: The const identifiers "_x" and "name" can be referenced in the same way as variable identifiers.

But: You cannot reassign a const. This provokes a compile-time error—the program will not compile.

VB.NET program that uses Const keyword Module Module1 ''' <summary> ''' Const can be Module or Class level. ''' </summary> Const _x As Integer = 1000 Sub Main() ' Const can be Function or Sub level. Const name As String = "Sam" ' Used as an argument. Console.WriteLine(name) Console.WriteLine(_x) ' Constant cannot be the target of an assignment. '_x = 2000 End Sub End Module Output Sam 1000
Performance. Fields and variables—declared with Dim—designate memory locations. The memory locations are on the stack (for locals) or the heap (for fields). When a Dim variable is evaluated, the execution engine reads from that memory location.
But with Const, the compiler inserts the value directly into the intermediate language representation. This means that when the method is executed, the value does not require a separate memory access. This slightly improves performance.

However: If you rewrite your VB.NET code to have Const values, but this increases code size, locality may be reduced.

And: This negates any performance wins. Code itself is data. Smaller code has an advantage.

Summary. Const values help clear up confusing code. They influence performance—often in a positive way. With Const we construct self-documenting code. A Const with the identifier "name" is known to be a name by any programmer reading the code.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf