title: Visual Basic: dynamically create objects using a string as the name
body: <p>Is there a way to dynamically create an object using a string as the class name?</p>
  
  <p>I've been off VB for several years now, but to solve a problem in another language, I'm forced to develop a wrapper in this one.  I have a factory method to dynamically create and return an object of a type based on input from elsewhere.  The provided input is meant to be the class name from which to create an object from.  Normal syntax means that the entire class has to be explicitly spelled out.  To do it this way, there could literally be hundreds of if/then's or cases to handle all the available class/object choices within the referenced libs:</p>
  
  <pre><code>If c_name = "Button" then obj = new System.Windows.Forms.Button
  If c_name = "Form" then obj = new System.Windows.Forms.Form
  ....
  </code></pre>
  
  <p>I'm hoping instead to reduce all this case handling to a single line:  IE...</p>
  
  <pre><code>my_class_name = "whateverclass"
  obj = new System.Windows.Forms.my_class_name()
  </code></pre>
  
  <p>In PHP, this is handled like so...</p>
  
  <pre><code>$my_class_name = "whateverclass";
  $obj = new $my_class_name();
  </code></pre>
  
  <p><strong>Edit:</strong> Looking at some of the answers, I think I'm in way over my head here.  I did manage to get it working using <a href="http://msdn.microsoft.com/en-us/library/dex1ss7c.aspx" rel="noreferrer">this CreateInstance</a> method variation of the Assembly class, even though I'm more interested in <a href="http://msdn.microsoft.com/en-us/library/ck6xe688.aspx" rel="noreferrer">this variation giving more options</a>, including supplying construct parameters...</p>
  
  <pre><code>my_type_name = "System.Windows.Forms.Button"
  asmb_name = "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
  button1 = Reflection.Assembly.Load(asmb_name).CreateInstance(my_type_name)
  </code></pre>
  
  <p>In other words, it takes a method to do this, and not any inherent language syntax?  <a href="http://msdn.microsoft.com/en-us/library/d133hta4.aspx" rel="noreferrer">This Activator variation</a> also worked when the full assembly string and class path is used.  I'm suspicious CreateInstance may not have the full ability to let me treat objects as if they were called normally, ie <code>obj = new System.Windows.Forms.Button</code>.  This is why I can't use simply <code>CreateObject</code>.  If there is no natural language feature allowing you to substitute a class name for a string, does anyone have any insight into what sort of limitations I can expect from using <code>CreateInstance</code>?</p>
  
  <p>Also, is there even a difference between basic Activator.CreateInstance (after Unwrap) and Assembly.CreateInstance methods?</p>