Shared Functions in Visual Basic

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 eample. 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"&'bect, e As Syste"&!#entArgs)andles Button1&Clic%  *abel1&+ext = Class1&Addition(+extBox1&+ext, +extBox&+ext)  !nd Sub !nd Class

Transcript of Shared Functions in Visual Basic

Page 1: Shared Functions in Visual Basic

8/12/2019 Shared Functions in Visual Basic

http://slidepdf.com/reader/full/shared-functions-in-visual-basic 1/6

Shared functions in Visual Basic .NETShared 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 eample. 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 Integer  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"&'bect,e As Syste"&!#entArgs)andles Button1&Clic%  *abel1&+ext = Class1&Addition(+extBox1&+ext, +extBox&+ext)  !nd Sub!nd Class

Page 2: Shared Functions in Visual Basic

8/12/2019 Shared Functions in Visual Basic

http://slidepdf.com/reader/full/shared-functions-in-visual-basic 2/6

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 dont 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, lets "et back to your post, what eample 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 dont 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 dont need to create an addition

function, a ToStrinfunction 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 dont have a "ood eample to show you here.

1mall pro"ram dont 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 eplain what shared functions with more or less details are. Others

"ive advanta"es and disadvanta"es but nothin" practical eample. /aybe someday, Ill make a

"ood and small eample. +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

Page 3: Shared Functions in Visual Basic

8/12/2019 Shared Functions in Visual Basic

http://slidepdf.com/reader/full/shared-functions-in-visual-basic 3/6

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

Page 4: Shared Functions in Visual Basic

8/12/2019 Shared Functions in Visual Basic

http://slidepdf.com/reader/full/shared-functions-in-visual-basic 4/6

  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

Page 5: Shared Functions in Visual Basic

8/12/2019 Shared Functions in Visual Basic

http://slidepdf.com/reader/full/shared-functions-in-visual-basic 5/6

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

eecution 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 instanceas the first argument.

S!mmar"

Page 6: Shared Functions in Visual Basic

8/12/2019 Shared Functions in Visual Basic

http://slidepdf.com/reader/full/shared-functions-in-visual-basic 6/6

We saw an eample 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 onwhether it acts upon

specific instances or not.