ARTICLE

Creation of objects using Late-Binding technique

Posted by Santhosh Veeraraman Articles | Visual Basic 2010 April 11, 2008
This article will explain how we can create objects in runtime, using late binding technique. Especially for a situation where you will come to know the class name only in runtime.
 
Reader Level:

Introduction

This article will explain how we can create objects in runtime, using late binding technique.

 

Late-Binding

 

Assume you are in a situation where you want to create object of a class only in runtime. Say for example, the class name is known to you only when you read it from an xml configuration file.

 

We can't instantiate it, because of the fact that we do not know which class has to be used. Late-binding technique will give you a hand in such kind of difficult situation.

 

Factory Method

 

We are using a factory method in this example, which will do the job for us. It will get the class name as parameter, create the object and give it back to you.

 

Code:
 

Imports System 

Imports EtchingFormExceptions
 

''' <summary>  

''' Factory Class  

''' </summary> 
 

Public NotInheritable Class Factory

    Private Sub New()

    End Sub
    ' This is a factory method that returns the required object

    ' using late binding.(i.e. the class is available only in runtime)

    Public Shared Function GetEmployee(ByVal Classname As String) As Employee

        Dim EmployeeObject As Employee

        Try

            ' Set this as a rule. You have to pass Namespace.Classname

            Dim FullName As String = "MyProjectNameSpace." + Classname

            Dim t As Type = Type.[GetType](FullName)

            ' Create instance using Activator class.

            Dim oTObject As Object = Activator.CreateInstance(t)

            ' Type cast it to our employee.

            EmployeeObject = DirectCast(oTObject, Employee)

        Catch generatedExceptionName As Exception

            Throw New ClassNotFoundException()

        End Try

        Return EmployeeObject

    End Function

End Class

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# Corner (http://www.c-sharpcorner.com/).

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor