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 Westi 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 VBComponent

Status
Not open for further replies.

oharab

Programmer
May 21, 2002
2,152
GB
I have a set of class modules all implementing the same interface.
Code:
Implements ISQL

I have a form that lists all classes that implement the interface in a combobox. So far so good!
What I want to do is create a new object based on what the user selects, but the closest I can get is the VBComponent.
Code:
Private downloadFile As ISQL

Private Sub cboSelectList_Change()
Set downloadFile = VBProj.VBComponents(cboSelectList.Text)
End Sub

What I'd like to do is "cast" that vbcomponent into a new object, but for the life of me, I can't work out how. Is this even possible? Oh for some reflection!!

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.
 
Hi Ben - did you mean to post this in the VBA forum - looks more like VB....

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Hi Geoff.

I'm doing it in Access & Excel, so it is VBA, but it's equally applicable to VB6, I guess.
I'll post in there too.

B.

----------------------------------------------
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.
 
You may get batter answers in there - given that I looked at that and went "wtf?"

Dunno if it'd work but might be worth 30 secs - have you tried enclosing the statement in the evaluate brackets?

Set downloadFile = [VBProj.VBComponents(cboSelectList.Text)]

Other than that I really don't have any ideas - this is at a level I think VBA rarely reaches - hence the suggestion to post in VB forum where I would imagine this type of code is a little more common...


Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Can you create a function that instantiates selected class?
Code:
Private Sub cboSelectList_Change()
Set downloadFile = NewObject(cboSelectList.Text)
End Sub

Private Function NewObject(ClassName as String) as ISQL
Select Case ClassName
Case "ClassName1"
    Set NewObject=New ISQL_1
Case "ClassName2"
    Set NewObject=New ISQL_2
...
End Select
End Function

combo
 
Hi combo,
I can, but the main reason I started using interfaces was so that new classes can be created by anyone & imported into the system, so people can have their own custom systems with the ISQL's they need.

Currently the only way I can see to make it work is to use VBE to write a new function every time it runs, but that seems dirty!

B.


----------------------------------------------
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.
 
In this case I would consider creating a procedure that manages importing/removing class modules. It would build a helper class that contains a list of available classes and one function that creates selected one.
Then CallByName or call to the only method of helper class would return required object.

combo
 
Thanks combo, I was kinda heading that way, but you helped cement the ideas in my head.
What I've got is a function that creates a "Select Case" procedure in a module every time the select form runs.

That way I can easily import & create new modules without having to remember to run them through a helper function.

Thanks again.

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top