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 Shared Function

Use the Shared modifier to specify that a member is not part of a type instance.
Shared. A Shared function is not part of an instance. Some functions and fields may be specific to an entire type, not an instance. The Shared modifier specifies that a function is meant for the entire type, not just a certain instance of it.Function
Example. This program introduces a Widget class. In the Widget class, we see a Public Shared Function and a Public Function. In the Main subroutine, we can call Widget.Test() because Test() is a Shared function. No instance is required.Sub

However: We must instantiate Widget, with New Widget(), to call TestB(). The TestB() function is not Shared.

Note: Fields, such as the _value field above, are also Shared or not Shared.

And: In a program, Shared fields can be accessed by Shared Functions. Regular Functions can access both Shared fields and also regular fields.

Thus: This means that Shared functions have additional restrictions on what they can do.

VB.NET program that uses Shared function Module Module1 Class Widget Public Shared Function Test() As Integer ' Cannot access regular fields. Return Integer.Parse("1") End Function Dim _value As String = "2" Public Function TestB() As Integer ' Can access regular fields. Return Integer.Parse(_value) End Function End Class Sub Main() ' Use Shared Function. Console.WriteLine(Widget.Test()) ' Use Non-Shared Function. Dim w As Widget = New Widget() Console.WriteLine(w.TestB()) End Sub End Module Output 1 2
Performance. The .NET Framework's execution engine needs to evaluate instances before an instance function can be called. For this reason, Shared functions (static in the C# language) have a tiny performance advantage.

Also: In many implementations, instance functions are passed a reference to the instance as the first argument.

Summary. We saw an example of a Shared function and a regular function. Shared members, often called static members, are part of the type, not part of the type's instances. They have advantages and also disadvantages.

But: You can determine whether a function should be shared based on whether it acts upon specific instances or not.

© 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