Instantiate class once on app start for use by whole app


Right now, I’ve got a GamersDB class which has methods to to access my database. I feel like this might be a bit slow, as I’m dimming the db class every time I try to lookup a record, and there are about 30 database calls on startup of my app.

Now, the question at hand:
Is there a way to put this in my Event handler and access it from other classes?

  Dim db As GamerDB
  db = new GamerDB()

Or should I continue to instantiate every time to use db.find() method?

Add a Module to your project.
Add a property “db as GamerDB” to the Module.
Change your app.Open code to simply

db = new GamerDB()

Remove all other “Dim db…” statements and use db wherever you want.

Nice, thank you. I just created an empty module with that property as you stated, and it worked.

How can I make this into a property of the same module? It has parameters.

Dim client As new MongoDriver.MongoClient(getDBHost, getDBPort)

Is that an OLE object of some kind? What is MongoDriver.MongoClient? How did you add it to your project?

MongoDriver is a module by Alwyn for MongoDB.

So is there a way to set a Module property with parameters?

Dim client As new MongoDriver.MongoClient(getDBHost, getDBPort)

I tried setting a property of “client” as “MongoDriver.MongoClient” for the module, but then it doesn’t work because of the two parameters I’m not passing (host, port).

You need to have the property declaration separate from the initialization of the object.
So in the module put a property

client as MongoDriver.MongoClient

and in the App.Open event put

client = new MongoDriver.MongoClient(getDBHost, getDBPort)