| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Re: Getting a class insta |
"Jeff" wrote in message
news:7b01eaf3.0407181946.2a91e542{at}posting.google.com...
> Hello,
>
> in my Access 2002 project, I have several classes which I'd like to
> instantiate through some kind of function that takes a string and
> returns the newly instantiated object.
>
> For example, if there's a class Cat:
> // in Cat class module:
> public sub meow()
> msgbox "Meow"
> end sub
> // end of Cat class module
>
> I'd like to be able to somehow create a function that returns a Cat
> object like the following:
>
> dim x as object
> set x = getClassInstanceByClassName("Cat")
> x.meow
> set x = nothing
>
>
> The trick is, I don't know what classes are going to be present in the
> project (this code is part of a testing framework I'm designing) ...
> if I did know, the function I want would be pretty easy:
>
> public function getClassInstanceByClassName(byval s as string) as
> object
> ' build a collection of the classes
> dim c as Collection
> set c = new Collection
> c.add new Cat, "Cat"
> ' ... etc, add more
>
> set getClassInstanceByClassName = c.Item(s)
> end function
>
>
> Is there some kind of API function I can call that does something like
> the following?
>
> public function getClassInstanceByClassName(byval s as string) as
> object
> ' get memory allocation/instantiation function location for an
> object
> ' of class name s
> ' do allocation of new object
> set getClassInstanceByClassName = the_new_object
> end function
>
>
> The testing framework currently works through VBA's pseudo-reflection
> (run time investigation of the classes in the project), but it has to
> write code in the project (self-modifying) to build the collection of
> classes. I don't want to have my project writing to itself ... while
> it works, it just seems a bit too hairy. Also, I don't want
> developers to have to go and add a line to the collection creation
> routine manually whenever they have the class, since I want this
> testing framework to be invisible to developers using it.
>
>
> Thanks,
>
> Jeff
I don't think that collection approach would work out well, since it
would contain exactly one instance of each class. Every time you asked
for a cat, you would get the same cat, and some users might want a whole
herd of them.
If the classes were full blown ActiveX classes, duly registered in the
registry with class IDs, you could use something like
Set NewObj = CreateObject("modulename.cat")
I don't think that classes created in Access can be ActiveX classes.
Even in VB6, it can be a pain to have to compile and install dlls to get
your all the classes publicly registered.
I'm not sure you can do better than to build a select case statement at
run time, with a
case "cat"
Set NewObj = New Cat
for each of the classes you find. You might be able to do that in the
user's project instead of in yours, so yours can be self contained.
In fact, I'd be interested in how you are retrieving the list of classes
at run time through VBA reflection. I have poked around just a little in
VB extensibility, and it has a VBComponents collection available at
design time which contains the defined classes, but I don't think it is
available at run time.
---
þ RIMEGate(tm)/RGXPost V1.14 at BBSWORLD * Info{at}bbsworld.com
---
* RIMEGate(tm)V10.2áÿ* RelayNet(tm) NNTP Gateway * MoonDog BBS
* RgateImp.MoonDog.BBS at 7/19/04 5:56:10 AM
* Origin: MoonDog BBS, Brooklyn,NY, 718 692-2498, 1:278/230 (1:278/230)SEEN-BY: 633/267 270 @PATH: 278/230 10/345 106/1 2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.