Saturday 15 February 2014

excel vba - Best practice when validating class properties in VBA? -



excel vba - Best practice when validating class properties in VBA? -

i haven't worked much classes, think beginner's question.

i have class has next property:

private pavtalsslut date '''''''''''''''''''''' ' avtalsslut property '''''''''''''''''''''' public property avtalsslut() date avtalsslut = pavtalsslut end property public property allow avtalsslut(value date) pavtalsslut = value end property

when set property values objects of class utilize next validation in subs:

if isdate(exportwks.cells(r, lcolumnavtalsslut)) avtal.avtalsslut = exportwks.cells(r, lcolumnavtalsslut) end if

i because otherwise error when cell read empty.

when property values objects of class utilize next validation in subs:

if avtal.avtalsslut <> 0 wunderlag.cells(row, 3) = avtal.avtalsslut end if

i because don't want write zeros there no dates. want leave cells blank in case.

now question. best practices these kinds of validations? should have themin class or in subs? if should in class, how should look?

(ps.is validation right vocabulary these kinds of checks?)

generally speaking, it's best allow class validate info before getting or setting property, , raise error if info doesn't meet specifications. way don't have repeat validation code on code base of operations anytime utilize class.

however, in case, you're trying avoid type mismatch error. so, need alter property type variant instead of date , you're going surprise , confuse uses class expecting give/receive dates. violates principal of to the lowest degree surprise , makes class useless if need utilize but reading/writing spreadhsheet.

so, neither. let's go alternative c.

create sec class (say, myclassreaderwriter) has job of acting intermediary between existing class , spreadsheet. allows 2 things.

you don't have create changes existing class, , remains useful in other contexts. it dries code placing logic 1 specific place.

vba excel-vba

No comments:

Post a Comment