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

Shared Members and Instance Members in VB.NET
Shared Members

By , About.com Guide

Nov 7 2009

The second type of member that an objects can have is called a shared member. The syntax for shared members is different. In particular, you don't have to create an instance of the object in your code to use a shared member. One of the best examples of shared methods can be found in the Math class that is part of the .NET Framework. Enter Math. in the VB.NET editor to see all of the methods and properties available. To take advantage of shared properties and methods, just code the object and the member directly. For example, to get the trigonometric tangent of a 45 degree angle:

MsgBox(Math.Tan((Math.PI / 180) * 45))

This give you the value 1. Note that the Math object trig functions have to be expressed in radians so both the shared property PI, to convert from degrees to radians, and the shared method Tan had to be used.

A major reason for using a shared member rather than an instance member is to avoid having to code a member for every class that might use essentially the same one. For example, the Format method of the String object is a shared method that you see a lot. Using this method, you can format a wide variety of data types such as dates and times, numbers and strings. Dim aDate As Date = #12/7/1941# Console.WriteLine(Format(aDate, "MMM dd, yyyy")) Dim aNum As Double = 123.4567 Console.WriteLine(Format(aNum, "##0.00"))

If Format was an instance method, it would have to be defined for every type instead. In other words, The DateTime type would have to have a Format method defined for it and the Double type would have another one. Then you could code something like: ' This doesn't work - example only Dim aDate As Date = #12/7/1941# Console.WriteLine(aDate.Format(#12/7/1941#)) Dim aNum As Double = 123.4567 Console.WriteLine(aNum.Format("##0.00"))

Shared properties are much less common than shared methods and it's not likely that you'll code very many in your own programs. The reason is that there are usually no constant values that are always the same whenever an object is used. One classic example of a shared property is the collection of connection parameters for a database when the code for an object needs to query a database. Since they're usually constant, these connection parameters for the call are often shared properties.

Here's a simplified example of a class that uses both shared properties and methods.

Suppose you had a class that was used to query an inventory database. A common requirement for the class would probably be to return the current inventory. This method doesn't require any parameters and would be the same for all programs. Note that the qualifications for the Function statement are the major difference.

Public Shared Function CurrentInventory() As Integer
   ' A database call would probably be
   ' coded in this method.
   ' For our purposes, we'll stub
   ' the method with a constant value.
   CurrentInventory = 12345
End Function

An example of a system that uses a shared property might be a system that normally uses inventory contained in a specific database. The database used could be enforced by coding the name as a shared property. (There are other ways to solve this problem too and I'm not claiming this is the best one.) One advantage (or disadvantage, since it falls into a category of a "hidden" function of the program) is that the database actually used depends on the version of the object library included. Different versions could reference different databases. One might include one database with phony numbers to generate a report for the auditors and another for owners. I'm not suggesting that you should of course, since that would be illegal:

Public Shared ReadOnly Property DBName() As String
   Get
      Return "FunnyNumbers_DB_for_the_auditors"
   End Get
End Property

You can reference the properties and methods for this custom object just like the ones in the .NET Framework:

MsgBox(Inventory.DBName)
MsgBox(Inventory.CurrentInventory)

It's just like you learned as a child, "It's good to share."

Explore Visual Basic
By Category
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. 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.