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

Shared and Instance Members in VB.NET
The difference between the two kinds of Class members

By Dan Mabbutt, About.com

Nov 7 2009

I'm reminded of the old programmer joke, "There are 10 kinds of programmers. Those who understand binary and those who don't."

In a similar way, there are 10 kinds of class members in VB.NET. Shared members and instance members.

But before we get into that, let's review the whole concept of classes and a class members. A class is the code for an object. Classes are really just collections of methods and properties that are bundled together for your convenience in using them. These methods and properties are collectively called the <b>class members</b>.

The code for a property in the class looks something like this:

Public Property aSampleProperty() As datatype
   Get
      ' code to retrieve the property
      Return property value
   End Get
   Set(ByVal value As datatype)
      ' code to initialize or update the property
   End Set
End Property

And the code for a method might look like this:

Public Sub aSampleMethodSub( _
   ByVal or ByRef aParameter As datatype))
      ' method code
End Sub

Or this:

Public Function aSampleMethodFunc( _
      ByVal aParameter As datatype)) _
      As datatype)
      ' method code
   aSampleMethodFunc = ReturnValue
End Function

After an object (that is, the compiled class code) is included in your project, you normally have to create an instance of the the object to use the members, something like this:

Dim myInstance as New aSampleObject

You can then use the methods and properties of the object:

myInstance.aSampleProperty = myVariable
myInstance.aSampleMethodSub(myVariable)
anotherVariable = myVariable
oneMoreVariable = myInstance.aSampleMethodFunc(myVariable)

These types of members are called "instance members" because you have to create an instance, a copy of the object exclusively for your code, to use the members. This is the most common code that you'll see and write when you code your own classes.

One way to prove to yourself that you're using a copy would be to code a method that contains a property and then change the property in copies of the object. For example code these members and in the class:

Private m_Property As Integer
Public ReadOnly Property testProperty() As String
   Get
      Return m_Property
   End Get
End Property
Public Sub TestMethod(ByVal Selector As Boolean)
   If Selector = True Then _
      m_Property = 99999 _
   Else _
      m_Property = 11111
End Sub

Then create two instances of the same class in the calling code:

Dim myInstance = New testClass
Dim anotherInstance = New testClass

Then set the value of the m_Property to different values in each of them and check them out:

myInstance.TestMethod(True)
anotherInstance.TestMethod(False)
MsgBox(myInstance.testProperty)
MsgBox(anotherInstance.testProperty)

Each instance of the object now contains a different property value.

On the next page, the differences between shared and instance members are described.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Shared Members and Instance Members in VB.NET

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

All rights reserved.