2

I haven't worked much with classes, so I think this is a beginner's question.

I have a class that has the following property:

Private pAvtalsslut As Date

''''''''''''''''''''''
' Avtalsslut property
''''''''''''''''''''''
Public Property Get Avtalsslut() As Date
    Avtalsslut = pAvtalsslut
End Property
Public Property Let Avtalsslut(Value As Date)
    pAvtalsslut = Value
End Property

When I set property values to objects of this class I use the following validation in my subs:

    If IsDate(exportWks.Cells(r, lColumnAvtalsslut)) Then
        avtal.Avtalsslut = exportWks.Cells(r, lColumnAvtalsslut)
    End If

I do this because otherwise I would get an error when the cell I read from is empty.

When I get property values from objects of this class I use the following validation in my subs:

    If avtal.Avtalsslut <> 0 Then
        wUnderlag.Cells(row, 3) = avtal.Avtalsslut
    End If

I do this because I don't want to write zeros where there are no dates. I want to leave cells blank in that case.

Now to my question. What are some best practices for these kinds of validations? Should I have themin my class or in my subs? If they should be in my class, how should that look?

(PS.Is validation the correct vocabulary for these kinds of checks?)

Community
  • 1
  • 1
user1283776
  • 19,640
  • 49
  • 136
  • 276
  • 2
    generally speaking the purpose of having class properties (*getters and setters*) is to do data validation on the incoming and outgoing data. Therefore, any data validation should *most of the time* be carried out inside the body of the property before actually assigning/returning a value. What is your actual question because best practices is too broad - everything depends on the situation you are facing. –  Nov 10 '14 at 15:40
  • Do you intend to use this class for anything other than reading/writing from spreadsheets? The answer really depends on knowing the intended use. – RubberDuck Nov 10 '14 at 15:53
  • @RubberDuck: I just want to read from one file and write to another. I would like the class to ignore reading and writing when date is nothing. – user1283776 Nov 10 '14 at 16:08

1 Answers1

3

Generally speaking, it's best to let the class validate the data before getting or setting a property, and then raise an error if the data doesn't meet specifications. This way you don't have to repeat that validation code all over your code base anytime you use your class.

However, in this case, you're trying to avoid a type mismatch error. So, you would need to change the property type to Variant instead of Date and then you're going to surprise and confuse anyone who uses your class expecting to give/receive dates. This violates the principal of least surprise and makes the class useless if you need to use it for anything but reading/writing to a spreadhsheet.

So, neither. Let's go for option C.

Create a second class (say, MyClassReaderWriter) that has the job of acting as an intermediary between your existing class and the spreadsheet. This allows for two things.

  1. You don't have to make any changes to your existing class, and it remains useful in other contexts.
  2. It dries up your code by placing the logic into one specific place.
RubberDuck
  • 11,933
  • 4
  • 50
  • 95
  • 2
    ++ and then [the next lecture](http://stackoverflow.com/questions/19881863/how-to-use-comparison-methods-between-class-object-modules-in-vba-in-a-similar-m) –  Nov 10 '14 at 16:50
  • 2
    oh and [one more](http://stackoverflow.com/questions/19373081/how-to-use-the-implements-in-excel-vba/19379641#19379641) –  Nov 10 '14 at 16:50