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