Initialize Custom Class from string

Posted on 2009-02-24
Last Modified: 2012-05-06
Hello Experts,
I am in need of some help. I have a class that is specific to a customer to enable and disable textboxes on a form. I am the class name is based off string values. I am trying to use System.Activator.CreateInstance to call the class and perform the behaviors.

I am getting an error "Could not load file or assembly 'ShowMe' or one of its dependencies. The system cannot find the file specified."

Please help.

Public gsOwnerClass As Object        

        Dim lsModuleName As Object = CType("ShowMe", Object)

        gsOwnerID = "ASDD"

        gsOwnerClass = "cls" & Strings.StrConv(gsOwnerID, VbStrConv.ProperCase) & "1"

        Dim assembly As String = lsModuleName.ToString

        Dim classname As String = gsOwnerClass.ToString

        Dim pc

        pc = System.Activator.CreateInstance(assembly, classname).Unwrap 'Error occurs here 


Public Class clsAsdd1

    Sub ShowMe()

        MsgBox("You got me", MsgBoxStyle.Critical, "You hit the class")

    End Sub

End Class

Open in new window

Question by:fullbugg
    LVL 53

    Expert Comment

    Following example can be found at

    Did you try the following (in your code)
    Dim oh As System.Runtime.Remoting.ObjectHandle = _
                Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase, _
    Dim st As SomeType = CType(oh.Unwrap(), SomeType)
    Module Module1
        Sub Main()
            ' Create an instance of the StringBuilder type using 
            ' Activator.CreateInstance.
            Dim o As Object = Activator.CreateInstance(GetType(StringBuilder))
            ' Append a string into the StringBuilder object and display the 
            ' StringBuilder.
            Dim sb As StringBuilder = CType(o, StringBuilder)
            sb.Append("Hello, there.")
            ' Create an instance of the SomeType class that is defined in this assembly.
            Dim oh As System.Runtime.Remoting.ObjectHandle = _
                Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase, _
            ' Call an instance method defined by the SomeType type using this object.
            Dim st As SomeType = CType(oh.Unwrap(), SomeType)
        End Sub
        Class SomeType
            Public Sub DoSomething(ByVal x As Int32)
                Console.WriteLine("100 / {0} = {1}", x, 100 \ x)
            End Sub
        End Class
    End Module

    Open in new window


    Author Comment

    Thanks for the reply...
    However the only way that I can make this work is by entering the class name in the code. I would like to do this by using a dynamically created string at runtime.

    Class Name would be created like:
    gsOwnerClass = "cls" & Strings.StrConv(gsOwnerID, VbStrConv.ProperCase) & "1"

    and used like:
            Dim oh As System.Runtime.Remoting.ObjectHandle = _
                Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase, _
    LVL 53

    Accepted Solution

    I figured it out in a small example

    You need to use the fully qualified name

    in example below, my projectname was WindowsApplication_VB_35
            Dim theClass As String = "WindowsApplication_VB_35.clsAsdd1"
            Dim _asm As Assembly
            _asm = Assembly.GetExecutingAssembly()
            Dim obj As Object = _asm.CreateInstance(theClass, True)

    Open in new window


    Author Closing Comment

    Thanks... that works perfectly... You are the coder.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now