Monday, November 5, 2012

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 example. To show simple the idea is.

Here is the class with a single shared function (Class1.vb):


Public Class Class1
    Shared Function Addition(ByVal x As Integer, ByVal y As Integer) As Integer
       Addition = x + y
    End Function
End Class



Here is the Form1.vb (again very simple):

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Label1.Text = Class1.Addition(TextBox1.Text, TextBox2.Text)
    End Sub
End Class



When you run the program, you will have a simple window form with 2 TextBox. Enter a value in each of them and press Button1 to get 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; Form1 then display it in the Label1.

Ok, what is the whole purpose of this thing? 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 suggest avoiding using the shared functions. They are the same people normally that will suggest to not use other “complicate thing” like the function friend and protected.

Ok, let’s get back to your post, what example I have for a good use of shared functions? Right now….  None. You could always make public stuff in a regular Module.

You don’t need to do basic public shared function because in Visual Studio or any Visual Basic Express Edition, you normally have all the functions you need. You don’t need to create an addition function, a ToString function… everything is in hidden somewhere in the Frameworks library.

This is my opinion:


You will need to create shared class the moment you create your own class and only if your customized class if big enough. Not only that, is only worth for it if you have many class and those class need to interact between them. Yet, I don’t have a good example to show you here.

Small program don’t require shared function. You could do anything with simple coding of your program is small. Shared functions are normally use for big program and more complex data manipulation and help the programmer reprogram similar functions over and over.

I look over the internet. Many explain what shared functions with more or less details are. Others give advantages and disadvantages but nothing practical example. Maybe someday, I’ll make a good and small example. Right now, I have nothing to comment.



References:


The program I love to use, buy it: Visual Studio 2010 Professional (Old Version)





2 comments: