Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instantiate a class by a string name?

Status
Not open for further replies.

BlackDice

Programmer
Mar 1, 2004
257
US
I'm trying to find out if I can have a class's name (like CPerson) stored in a database field, then instantiate the class by just having the name of the class as a string?

so instead of

Code:
set objPerson = new CPerson

I could have something like

Code:
set objPerson = new ConvertFunction("CPerson")

BlackDice

 
Set objPerson = CreateObject("CPerson")

Or

Dim ClassObject As String

ClassObject = "CPerson"
Set objPerson = CreateObject(ClassObject)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I believe you must use

CreateObject("ProjectName.ClassName")

I don't think you can use the class name stand alone.
 
I assume you are trying to instantiate a class contained in a DLL. My own project does this, so I know it works. It's just a matter of figuring out why you are having a problem in your own project.

The dll you are trying to access must be registered.

I have several report dll's in my app. Each dll has several class modules, each class module contains exactly 1 report. Each report is called identically (the name function name with the same parameters). The only parameter I pass in is... ConnectionString.

So, I have something like...

Code:
Public Sub PrintReport(ByVal ReportInstance As String)
  Dim oReport As Object

  Set oReport = CreateObject(ReportInstance)
  Call oReport.PrintReport(globalConnectionString)
  Set oReport = Nothing
End Sub

Every report has a PrintReport method that takes a connection string to the database. Examples of the ReportInstance parameter are... "bbReportStudent.BySchoolByGrade", and "bbReportStudent.HomeRoom", and "bbReportSchool.StudentCount"

Hope this helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Now it's beginning to make sense. CreateObject digs in to the registry to find the object. Since it's in your own project, it does not appear in the registry. Hence the "ActiveX component cannot create object" error message.

If you only have a small number of class modules to deal with, you could take the Select Case approach

Select Case strObjectName
Case "CPerson"
Set oObject = New CPerson
Case "CTruck"
Set oBoject = New CTruck
End Select

By the way, I know my limitations. There may be a way to do what you are looking for, but I don't know it.

Hope this helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Try GetObject instead of CreateObject.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Did you try putting your project name in to form a complete progid?

Set myobj = CreateObject("Myproject.MyClass")

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top