Visual Basic

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

Shared Members and Instance Members in VB.NET

There are two kinds of class "members" in VB.NET

By Dan Mabbutt, About.com

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 class members.

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.

SideBar Topic

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.

End SideBar Topic

Explore Visual Basic

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