How to Use Class Libraries in Visual Studio

  • google plus

In Brief...

The steps below describe how to utilize class libraries in Visual Studio.

Instructions

  1. A class library is a collection of class definitions contained in a .dll or .exe file.
  2. In order to use the class library, you must first add a reference to the library (see "How to add references to your Visual Studio Project").
  3. Once the library has been referenced, the classes in the library can be instantiated and exercised. In order to instantiate the class, you must either fully qualify the class name or bring it into scope with a using statement (C#) or Import statement (VB). To fully qualify the class name, you reference the library name and class name using dot syntax. If, for example, the library is named 'MyLib' and the class is named "MyClass", you would use the following syntax:
    C#
    
    	MyLib.MyClass = new MyLib.MyClass();
    VB
    Dim newObject as MyLib.MyClass()
  4. If you prefer, you can bring the namespace into scope with a 'using' or 'import' statement and then reference the class directly. For example,
    C#
    using MyLib;	 
    	class NewClass;
    		{
    			static void Main(string[] args)
    				{
    					MyClass = new MyClass();
    				}
    		}
    VB
    
    	imports MyLib	
    	Sub Main()
    		Dim newObject as MyClass()
    	End Sub
    	
  5. After instantiating the class, you can then use it as you would any object (exercise the properties, methods, etc.).

Author: Michael Osborne

Discuss