Introduction to Class Programming

Properties Of a Class

Now is the time to make our class to more robust class. A robust class is one that actively protects its internal data from tampering. So how can a class protect itself from invalid assignments, such as an empty string for its FirstName or LastName properties. To accomplish this purpose, you must change the internal implementation of the class module, because in its present form you have no means of trapping the assignment operation. Simply change all the Public member into Private members and encapsulate them in pairs of Property procedures.

  • To change our Student class 
  1. Double click the class Student.cls in the Project Explorer 
  2. In the Student Class Module, change all word Public to Private and add a prefix m_ in front of all private variables, as shown below: 
'In the declaration section of the Student class module
Private m_Student_ID As String
Private m_FirstName As String
Private m_LastName As String
Private m_YearLevel As String
Private m_BirthDate As Date        

NOTE
You can also use Replace Dialog box. To do this, press Ctrl-H, the Replace Dialog box appears. On the Find What combo box, type Public. Next on the Replace With combo box, type Private, then click Replace All button. 

Appending the prefix m_ is just a personal style, this way it keeps my property name and private member variable synchronize and it is commonly used in programming. Feel free to use it or to create your own style.

  1. In the Student Class Module, type the following code: 
'In the declaration section of the Student class module
Private m_Student_ID As String
Private m_FirstName As String
Private m_LastName As String
Private m_MajorCode As String
Private m_YearLevel As String
Private m_BirthDate As Date

Property Get MajorCode() As String
    MajorCode = m_MajorCode
End Property

Property Let MajorCode(ByVal strNewValue As String)
    ' Raise an error if an invalid assignment is attempted.
    If Len(strNewValue) = 0 Or Len(strNewValue) > 1 Then Err.Raise 5
    m_MajorCode = strNewValue
End Property

Property Get FirstName() As String
   FirstName = m_FirstName
End Property
   
Property Let FirstName(ByVal strNewValue As String)
  ' Raise an error if an invalid assignment is attempted.
  If Len(strNewValue) = 0 Then Err.Raise 5 ' Invalid procedure argument
  ' Else store in the Private member variable.
  m_FirstName = strNewValue
End Property
    
Property Get LastName() As String
   LastName = m_LastName
End Property

Property Let LastName(ByVal strNewValue As String)
   ' Raise an error if an invalid assignment is attempted.
   If Len(strNewValue) = 0 Then Err.Raise 5      ' Invalid procedure argument
   ' Else store in the Private member variable.
   m_LastName = strNewValue
End Property

Property Get StudentID() As String
   StudentID = m_Student_ID
End Property

Property Let StudentID(ByVal strNewValue As String)
   ' Raise an error if an invalid assignment is attempted.
   If Len(strNewValue) = 0 Then Err.Raise 5     ' Invalid procedure argument
   ' Else store in the Private member variable.
   m_Student_ID = strNewValue
End Property

Property Get BirthDate() As Date
   BirthDate = m_BirthDate
End Property

Property Let BirthDate(ByVal datNewValue As Date)
   If datNewValue >= Now Then Err.Raise 1001, , "Future Date!"
     m_BirthDate = datNewValue
End Property

Property Get YearLevel() As String
   YearLevel = m_YearLevel
End Property

Property Let YearLevel(ByVal strNewValue As String)
  Dim varTemp As Variant
  Dim found As Boolean
  
  For Each varTemp In Array("Freshmen", "Sophomore", "Junior", "Senior")
    If InStr(1, strNewValue, varTemp, vbTextCompare) Then
      found = True
      Exit For
    End If
  Next

  If Not found Then Err.Raise 5
  m_YearLevel = strNewValue
End Property
NOTE
Visual Basic can help you in typing Property Procedure by Add Procedure command from the Tools menu, which creates a templates for Property Get and Let procedures. But you should edit the result to a proper data type, because all properties created by this command is of type Variant.
  1. On the Run menu in Visual Basic, click Start
  2. When the program is running, click the Command1 button. 
  3. Click OK to close the message box. 
  4. On the Run menu in Visual Basic, click Stop

Everything works as before. What we have done, is make the class a bit more robust because it now refuses to assign invalid values to its properties. To see what I mean, just try to issue this command:

objStudent.FirstName = ""	'Raises an error 'Invalid Procedure call

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou