Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

Using Shared Access in Visual Basic .NET

Sharing makes all the difference in VB.NET!

By Dan Mabbutt, About.com

When you "instantiate" an object in Visual Basic, you simply make a copy of that object for use in your code. Making a copy of the object keeps each object independent; you can make a change to a property in one copy and not affect the others.

One way to illustrate the difference between an object and an instance of that object is to simply look a little closer at what happens when you declare a variable. You can't assign a value directly to a "type." In other words, you can't code:

Integer = 5 ' error

You have assign a value to a object is the same type that is part of your program.

Dim myInteger As Integer
myInteger = 5 ' ok

In the same way, you can't assign a value to a property of an object. You have to assign the value to the property in a copy of that object. If your program references an object called PoliticalParty, you can't write:

PoliticalParty.ElectionResult = "Won Election!"

Which party? It does make a difference. So you have to instantiate a PoliticalParty object and then change the property:

Dim myParty as new PoliticalParty
myParty.Name = "BullMoose"
myParty.ElectionResult = "Won Election!"

Or do you . . . ??

Let's write a program to vote in an election. Here's the form we're going to use:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

To keep track of parties and votes, I wrote this class:

Public Class PoliticalParty
   Private Shared _WhigVotes As Integer = 0
   Private Shared _BullmooseVotes As Integer = 0
   Private _partyName As String
   Private _ourSecretSlogan As String
   Private _pswd As String

   Public Sub New(ByVal partyName As String, ByVal slogan As String, ByVal pswd As String)
      _partyName = partyName
      _ourSecretSlogan = slogan
      _pswd = pswd
   End Sub
   . . . other members of the class ...
End Class

Note that there are two Shared variables and three non-Shared Private variables. What this means is that the values of the Shared variables are available to every copy of the object ("instance" is usually used instead of "copy"). Since they're Private, you still have to access them through an object method (a Function or a Sub in the object).

Since we're holding an election, it only makes sense that there is only one tally of the votes. (We have enough trouble with elections.)

Political parties have secrets, so when we instantiate the object for each party, a separate and unique instance of the non-Shared variables, including their secret slogan and their password, will be created.

Here's how Election Headquarters displays the vote count:

whigVotesDisplay.Text = _
   PoliticalParty.reportVotes("Whig")
bullmooseVotesDisplay.Text = _
   PoliticalParty.reportVotes("Bullmoose")
reportedByDisplay.Text = "Election Headquarters"

Notice now that the name of the object itself - "PoliticalParty" - is used to qualify the method call to reportVotes. No instantiation is required.

But when a political party accesses the object, they have to create their own copy. The object initializes the non-Shared variables in the New subroutine.

Public Sub New( _
   ByVal partyName As String, _
   ByVal slogan As String, _
   ByVal pswd As String)
   _partyName = partyName
   _ourSecretSlogan = slogan
   _pswd = pswd
End Sub

And when a party accesses the object, they can access their own unique information and the Shared variables.

Private Sub reportWhigResults_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles reportWhigResults.Click
   whigVotesDisplay.Text = _
      WhigParty.reportVotes("Whig")
   bullmooseVotesDisplay.Text = _
      WhigParty.reportVotes("Bullmoose")
   reportedByDisplay.Text = _
      WhigParty.reportedBy
   ourSecretSlogan.Text = _
      WhigParty.slogan(pswd.Text)
End Sub

Note that the password ("pswd") is necessary to access the secret slogan for the party.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The object processes the code like this:

Public Function slogan(ByVal pswd) As String
   If pswd = _pswd Then
      slogan = _ourSecretSlogan
   Else
      slogan = "None of yer business!"
   End If
End Function

Visual Studio puts a blue line under the code "WhigParty.reportVotes" as shown below with a warning, "Access of shared member through an instance; qualifying expression will not be evaluated."

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The "qualification" is "WhigParty". A qualification implies that unique information will be kept in a copy of the object, but we know this isn't the case here. Visual Studio is simply making sure you understand that. The code compiles anyway.

The purpose of this article is to explain how Shared works. The example here isn't intended to be a model of code you should use. (And you can probably think of better ways to accomplish the things this example does.) Microsoft strongly discourages the use of a Shared member in an instance expression even though it does work. Here's what they say about it:

"Accessing a Shared member through an instance variable can make your code more difficult to understand by obscuring the fact that the member is Shared. Furthermore, if such access is part of an expression that performs other actions, such as a Function procedure that returns an instance of the shared member, Visual Basic bypasses the expression and any other actions it would otherwise perform."

Download the Code!

More Visual Basic Quick Tips

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Quick Tips
  5. Shared and instances of classes in Visual Basic - instantiation and objects in VB.NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.