Converting String to Class Type

olsonpm

Well-known member
Joined
Aug 24, 2010
Messages
46
Programming Experience
Beginner
So after reading plenty on the CreateInstance method, and the Type.getTypeFromProgID method, I thought I had it figured out. However I keep getting a null reference returned when trying to recieve the type. I simply have a Class1, inside a Project1 (with the default namespace of "Project1"). Now from my understanding, I should be able to complete a line of code like this

VB.NET:
Dim myType as Type = Type.GetTypeFromProgID("Project1.Class1")

this keeps returning null though. I have been messing around with this for a good 2 hours, any help would be much appreciated.

thanks,
Phil
 
Wow, I'm sorry, but apparently I just needed to sift through some more examples. The answer is simply to use Type.GetType("<namespace>.<classname>"). I still don't understand why the above method didn't work, because from what i've read, namespace+classname should be the progid. Bleh.
 
You should start by reading the documentation:
(The GetTypeFromProgID) method is provided for COM support. ProgIDs are not used in the Microsoft .NET Framework because they have been superseded by the concept of namespace.
 
I'm sorry if i'm taking this the wrong way, but your comment comes off as slightly condescending. The documentation provided by visual studio displays this.

Public Shared Function GetTypeFromProgID(ByVal progID As String) As System.Type
Member of System.Type
Summary:
Gets the type associated with the specified program identifier (ProgID), returning null if an error is encountered while loading the System.Type.

Parameters:
progID: The ProgID of the type to get.

Return Values:
The type associated with the specified ProgID, if progID is a valid entry in the registry and a type is associated with it; otherwise, null.

Exceptions:
System.ArgumentException: progID is null.

For being new to Visual Studio, I would think that trusting it's own documentation isn't a bad thing to do. What source did you get that from?
 
Type.GetTypeFromProgID Method (String) (System) jmcilhinneys quote is from Remarks section.

ProgID is also a COM term, if you knew about progid's from before.

From the original post (which I first read today, after replies), there could be confusion since you seemed determined to use progids, but it also didn't seem quite right since you also talked about classes and namespaces. Anyway, you sorted it out. :)
 
Yeah - I have never used them before, but after reading I thought it was the correct way to go. Nevermind that, I'll do some more reading to get a better understanding. Thanks for the help.
 
Back
Top