Sie sind auf Seite 1von 6

Shared functions in Visual Basic .

NET
Shared functions in Visual Basic .NET

The function Shared helps you share function and variables between others class in your project. On a previous post, I show that a class with shared stuff acts almost the same way than a Module. We know that stuff in a module is available everywhere in your project. (Well, I know is not always true, but I love to say that is true). I made a really simple code and a really simple e ample. To show simple the idea is.

!ere is the class with a sin"le shared function (Class1.vb)#

Public Class Class1 Shared Function Addition(ByVal x As Integer, ByVal y As Integer) As Inte ger Addition = x y !nd Function !nd Class

!ere is the Form1.vb (a"ain very simple)#


Public Class For"1 Pri#ate Sub Button1$Clic%(sender As Syste"&'b(ect, e As Syste"&!#entArgs))andles Button1&Clic% *abel1&+ext = Class1&Addition(+extBox1&+ext, +extBox,&+ext) !nd Sub !nd Class

When you run the pro"ram, you will have a simple window form with $ TextBox. %nter a value in each of them and press Button1 to "et the results in Label1. Well you see it simply takes the value of each TextBox and send them in the Class1. The shared function processes that information and returns it in the Form1& Form1then display it in the Label1. Ok, what is the whole purpose of this thin"' Why complicate your life with shared functions' Well believe or not, some people don(t find it useful. In fact, to be honest many people su""est avoidin" usin" the shared functions. They are the same people normally that will su""est to not use other )complicate thin"* like the function friend and protected. Ok, let(s "et back to your post, what e ample I have for a "ood use of shared functions' +i"ht now,. -one. .ou could always make public stuff in a re"ular /odule. .ou don(t need to do basic public shared function because in 0isual 1tudio or any 0isual 2asic % press %dition, you normally have all the functions you need. .ou don(t need to create an addition function, a ToStrin function, everythin" is in hidden somewhere in the 3rameworks library.

This is m! o"inion#
.ou will need to create shared class the moment you create your own class and only if your customi4ed class if bi" enou"h. -ot only that, is only worth for it if you have many class and those class need to interact between them. .et, I don(t have a "ood e ample to show you here. 1mall pro"ram don(t re5uire shared function. .ou could do anythin" with simple codin" of your pro"ram is small. 1hared functions are normally use for bi" pro"ram and more comple data manipulation and help the pro"rammer repro"ram similar functions over and over. I look over the internet. /any e plain what shared functions with more or less details are. Others "ive advanta"es and disadvanta"es but nothin" practical e ample. /aybe someday, I(ll make a "ood and small e ample. +i"ht now, I have nothin" to comment.

VB.NET 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.

Example

This program introduces a Widget class. In the Widget class, we see a Public Shared Function and a Public Function. In the ain subroutine, we can call Widget.Test!" because Test!" is a Shared function. #o instance is re$uired. However:We must instantiate Widget, with #ew Widget!", to call Test%!". The Test%!" function is not Shared.
Program that uses Shared function: VB.NET

Module Module1

Class Widget Public Shared Function Test() As Integer ' Cannot access regular fields. Return Integer.Parse("1") nd Function

!i" #$alue As %tring & "'"

Public Function Test(() As Integer ' Can access regular fields. Return Integer.Parse(#$alue) nd Function nd Class

%ub Main() ' )se %*ared Function. Console.Write+ine(Widget.Test())

' )se ,on-%*ared Function. !i" . As Widget & ,e. Widget() Console.Write+ine(..Test(()) nd %ub

nd Module

Output

1 '

Fields, such as the &'alue field abo'e, are also Shared or not Shared. In a program, Shared fields can be accessed by Shared Functions. (egular Functions can access both Shared fields and also regular fields. Note:This means that Shared functions ha'e additional restrictions on what they can do.

Performance
The .#)T Framewor*+s e,ecution engine needs to e'aluate instances before an instance function can be called. For this reason, Shared functions !static in the -. language" ha'e a tiny performance ad'antage. lso:In many implementations, instance functions are passed a reference to the instance as the first argument.

S!mmar"

We saw an e,ample 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 ha'e ad'antages and also disad'antages. B!t:/ou can determine whether a function should be shared based on whether it acts upon specific instances or not.

Das könnte Ihnen auch gefallen