VBA - Is it possible to pass an Object's property as an argument in a method? -


i'm pretty used vba, not objects though , i'm hitting wall right now...

my config class has 100 properties, i'll not spam them here details doesn't matter question.

i hoped code duplicate function, create multiple objects 1 , assign different values specific property of each new objects (add new elements configurations, generates new configs), :

public function duplicate(srccfg config, propertyname string, properties string) collection dim cc collection, _     cfg config, _     totalnumber integer, _     a() string  set cc = new collection = split(properties, "/") totalnumber = ubound(a)  = 0 totalnumber     'create copy of source object     set cfg = srccfg.copy     'set property particular copy     cfg.propertyname = a(i)     'add copy collection     cc.add byval cfg next      duplicate = cc end function 

but i'm not sure collection best output (as i'll take results , incorporate them master collection), i'm open suggestions.

and i'm pretty sure we can't pass property argument (i spent quite times looking solution this...) , don't know super practical me. if there solution or workaround, i'll gladly try it!

here rest of methods :

friend sub setconfig(srcconfig config)     config = srcconfig end sub  public function copy() config     dim result config     set result = new config     call result.setconfig(config)     set copy = result end function 


final code duplicate object :


working smoothly :

private cfg config  friend sub setconfig(srcconfig config)     set cfg = srcconfig end sub  public function copy() config     dim result config     set result = new config     call result.setconfig(cfg)     set copy = result end function  public function duplicate(propertyname string, properties string) collection dim cc collection, _     cfg config, _     totalnumber integer, _     a() string  set cc = new collection = split(properties, "/") totalnumber = ubound(a)  = 0 totalnumber     'create copy of source object     set cfg = me.copy     'set property particular copy     callbyname cfg, propertyname, vblet, a(i)     'add copy collection     cc.add cfg next      set duplicate = cc end function 

you got right, including types (string).

just replace your

cfg.propertyname = a(i) 

with

callbyname cfg, propertyname, vblet, a(i) 

the property name must passed string, not reference or lambda or anything, no type safety or compiler aid here. have runtime error if misspell name.

as return type, vba not have lists, collection fine, because in particular case know in advance how many objects returning, can declare array:

dim cc() config redim cc(1 totalnumber) 

you declare array in case, if didn't know total number, you'd reallocating on every iteration.


Comments