TIP: Click on subject to list as thread! ANSI
echo: visual_basic
to: All
from: Jeff
date: 2004-07-19 06:13:00
subject: Getting a class instance

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
---
þ 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 6:13:55 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™.