[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

Initialize Custom Class from string

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 
 
CLASS:
Public Class clsAsdd1
 
    Sub ShowMe()
        MsgBox("You got me", MsgBoxStyle.Critical, "You hit the class")
    End Sub
 
End Class

Open in new window

0
fullbugg
Asked:
fullbugg
  • 2
  • 2
1 Solution
 
DhaestCommented:
Following example can be found at http://msdn.microsoft.com/en-us/library/system.activator.aspx

Did you try the following (in your code)
Dim oh As System.Runtime.Remoting.ObjectHandle = _
            Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase, _
                                         GetType("clsAsdd1").FullName)
Dim st As SomeType = CType(oh.Unwrap(), SomeType)
st.ShowMe()
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.")
        Console.WriteLine(sb)
 
        ' 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, _
                                         GetType(SomeType).FullName)
 
        ' Call an instance method defined by the SomeType type using this object.
        Dim st As SomeType = CType(oh.Unwrap(), SomeType)
 
        st.DoSomething(5)
    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

0
 
fullbuggAuthor Commented:
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, _
                                         GetType(gsOwnerClass).FullName)
0
 
DhaestCommented:
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)
 
        obj.ShowMe()

Open in new window

0
 
fullbuggAuthor Commented:
Thanks... that works perfectly... You are the coder.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now