How to create common/shared instance in vba -


i create 1 instance of class other use far create each instance in each class needes methods.

is there way create 1 instance seen other classes.

normaly create sort of static variable seen others. seems not possible in vba :/

in module:

private objsharedclass myclass  public function getshared() myclass      if objsharedclass nothing         set objsharedclass = new myclass     end if      set getshared = objsharedclass  end function 

it's vb(a) implementation of singleton pattern. need consider whether or not it's appropriate, , if so, that's way it. when use it, put above code in module (except if i'm using more 1 singleton in application put them in single module). can add destruction routine same module , call application exit:

public sub closesingleton()      set objsharedclass = nothing  end sub 

or can let go out of scope when app closes--not tidy, i've never seen cause problem (i clean up, though...).

edit

usage (just in case it's not obvious). either:

... set objlocalcopy = getshared dosomethingwith objlocalcopy.methodorproperty ... 

or:

... dosomethingwith getshared.methodorproperty ... 

the first preferable if you're going use shared class more once in calling routine, second works fine single call.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -