Library tutorials & articles

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
AddThis

Comments

  1. 07 Jun 2008 at 11:55

    [quote user="Developer Fusion Bot"]

    This thread is for discussions of Introduction to Class Programming.

    [/quote]

    This was the best explanation to "class", I have ever come across. Hats off to you.

  2. 12 Jul 2007 at 12:36

    Hello Sir,

    I have just seen u'r examples for class module.u'r explaination is simply superb!!!!

    now i clearly understood the class module concept in VB.

     

    Thanks

    Bhavani Josyula

  3. 15 May 2007 at 16:10

    This was a very good fundamental article on the use of classes in VB. looking forward to additional more advance content

  4. 16 May 2006 at 11:35

    It s a great sample about the class programming. However, it s still uncertain for me where I can use this in real life.

    Can anyone give me an example ?

  5. 05 Oct 2005 at 19:12

    what kind of help you wants from me. i mean to clear out your visual basic basics by giving you some tutorials or anything else. bye

  6. 29 Sep 2005 at 12:25

    hello sir, can you teach me the visual basic programing and where to start because i don't have a basic in this software. before this, i was try to learn this programe from e_book but it's looking so hurt to learn without a teacher.
    I would appericiate if u can show me the way to learn this programe.
    TQ

  7. 16 Jul 2005 at 02:59

    Thanks, Dante Salvador, for your excellent and lucid tutorial on Class Programming.
    I have worn Google to a frazzle looking for help. most of which was comprised of a few samples.
    You have helped "elucidate" me with both the why and how-to and that is no small task.
    Thanks so much for your efforts and help.

  8. 09 Jun 2005 at 17:34

    What a tutorial !! man i like it. this tutorial shows me another side of visual basic wow!!

  9. 23 Dec 2004 at 13:40

    This is really a good tutorial on Visual Basic. I was really coinfused between let, get properties. But this is the final place which solved ll my problems

  10. 22 Oct 2003 at 11:52

    vbexplorer.com has also an excellent oop tutorial, if youre looking for some more,
    and I have found a german article at vbarchiv.de that seems very good (i can
    tell because I studied german when in grad. school, swe).


    Maybe I should try an translate it to english...

  11. 20 Sep 2003 at 06:32

  12. 10 Sep 2003 at 17:41

    At one time I studied programming in Pascal & Fortran --- have forgotten lots of basics....I have read thru many VB tutorials etc but never have I so clearly had the 'light bulb' go on.  Thank-you for a well organized presentation.  Look forward to your future articles.

Leave a comment

Sign in or Join us (it's free).

Related discussion