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 Module Example: Shared Data

Use a module with the Module block. Members in a Module are shared.
Module. A Module is not a class. It is a container element, but is not an object. In a Module, all members are shared and have "Friend" accessibility. We cannot instantiate a Module—it serves mainly to organize code in a global, single place.
This example uses two modules. By default, the Sub Main is placed in a Module called "Module1." This is where control flow begins in a VB.NET program. I also introduce a second module, Module2.

Module2: This module has a field, _value, which is shared automatically (implicitly). So the field has only one instance.

Sub: In Module2 we see a Sub called Increment. This is by default "Friend." It can be easily accessed from Main.

Increment: When Increment is called, the field _value is changed. This change persists throughout uses of Increment.

Note: This demonstrates that the field is shared, even though it has no shared keyword.

Shared
VB.NET program that uses modules Module Module1 Sub Main() ' Use Module2. ' ... It does not need to be created. Module2.Increment() Module2.Increment() Module2.Increment() End Sub End Module Module Module2 Dim _value As Integer Sub Increment() ' The value is shared. Console.WriteLine(_value) ' Change the value. _value += 1 End Sub End Module Output 0 1 2
Error. We cannot instantiate an instance of a Module. There is no "New" Sub on it. A Module is not a type, but rather an organizational namespace for programs that is shared. It is another syntax form.

Caution: If you try to create a Module, you will get an error. Please consider adding a class to fix this problem.

Error: Module 'Module2' cannot be used as a type.
Discussion. A Module has many rules. For example, it cannot inherit from another module. And you cannot specify the shared keyword on any of its members (they are already shared). In general, modules are simple to use.
Summary. Modules are common in VB.NET programs. But more complex data structures are not built with many modules—rather they use classes as building blocks. A module is not object-oriented. It is an organizational, shared-data construct.Class
© 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