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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create new object from string

Status
Not open for further replies.

oharab

Programmer
May 21, 2002
2,152
GB
Hi
Sorry this is a cross post (thread707-1491214).
Basically,
I have a list of classes and I want to create a new object from a user selected variable
Code:
Private downloadFile As ISQL

Private Sub cboSelectList_Change()
Set downloadFile = [b]cboSelectList.Text[/b]*

*Do something with this!!!!
End Sub

Any thoughts on how I may do this?

Ben

----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
Good idea strongm, annoyed with myself for not thinking of it.


Tried it & turns out it only creates ActiveX objects, not plain old code objects :(



----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
But your original example at the top of the thread strongly suggested that it was an ActiveX object that you wanted ...

So now I'm confused about what you want to achieve.
 
Sorry strongm, I didn't make myself particularly clear.

This is all done in VBA, specifically MS Access, but it should be transferable between VB6 & VBA.
What I have are several classes in my project, all implementing a common interface (ISQL in this case).

In a combo box I have a list of the class names for the user to select.

When the "GO" button is pressed, I'd like to instantiate a new object based on the selected class. I can do a "Select Case" routine:
Code:
Dim download as ISQL

    Select Case Me.cboDownloadType.Value
        Case "BurglaryDwelling"
            Set downloadFile = New BurglaryDwelling
        Case "CriminalDamage"
            Set downloadFile = New CriminalDamage
        Case "VehicleCrime"
            Set downloadFile = New VehicleCrime
        Case "SeriousViolence"
            Set downloadFile = New SeriousViolence
        Case "OtherViolence"
            Set downloadFile = New OtherViolence
        Case "YouthOffenders"
            Set downloadFile = New YouthOffenderCrimes
        Case "YouthVictims"
            Set downloadFile = New YouthVictimCrimes
        Case "AllCrimesInLeeds"
            Set downloadFile = New AllCrimesInLeeds
        Case "FOI2008127136"
            Set downloadFile = New FOI2008127136
    End Select

But this gets messy as I add more classes.
Ideally what I'd like to do is use a similar construct to
Code:
    Eval "Set downloadFile = New " & Me.cboDownloadType.Value

but that won't work.
As a dirty hack, I'm trying to edit the VB project dynamically to create a procedure to return the correct object, but it's not pleasant and I'm not sure how well it will scale or how having multi-users will effect it.

Hope this makes things a little clearer for you & you have a flash of inspiration! I know if I was in .NET, I could use reflection, but that's not available to me in VBA/VB6!

Many thanks

Ben


----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
>that's not available to me in VBA/VB6!


Well, it is (sort of) via the TypeLib Information library. However, this doesn't work directly against classes included internally in a project, only if they are in an ActiveX exe, dll or a TLB or OCX (oh, and can also grab info from running objects).

There's a trick we can use to get the info for internal project classes - but I don't think it ends up being any cleaner than your Select statement ...
 
Thanks strongm.
Because it's in VBA, I can manipulate the code within the project (not sure if that's possible in VB6), so I can dynamically create my Select statement based at runtime. It's not pretty, but it works, and that's 9/10ths of the battle!

Cheers

Ben

----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
oharab said:
What I have are several classes in my project, all implementing a common interface (ISQL in this case).
Does everything you need to do with downloadFile use methods and properties of the ISQL interface? If so, you could just declare the object as ISQL.

If that doesn't work, my next suggestion would be to put all the methods/properties needed for downloadFile in a new interface, let's say IDownloadFile, and have every class implement that interface.


Joe Schwarz
Custom Software Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top