Library tutorials & articles

Introduction to Class Programming

Read-Only Property

If you look at Visual Basic how it handle its own object, such as form and controls, some properties can be both read and be written to.  For example, you cannot modify the Height property of a ComboBox even at design time and you cannot modify the MultiSelect property of the ListBox at run time. You can also use this technique to limit the access to your class properties, thus making them read-only.

You can make a property to be read-only property by simply omitting its Property Let procedure.  For example, we might add a FullName property to our Student class.

Public Property Get FullName() As String
  ' Raise an error if an FirstName or LastName is empty
   If (Len(m_FirstName) = 0) Or (Len(m_LastName) = 0) Then Err.Raise 5
  ' Else return the Student Fullname
   FullName = m_FirstName & " " & m_LastName
End Property

Now test your read-only property.  Try to issue a command like as shown below:

'this raise an error Compile Error:  Cannot assign to read-only property
objStudent.FullName = "Samantha Aniversario"

Visual Basic raises a Compile Error "Cannot assign to read-only property", because you are trying to assign a value to a read only property.  Your program won't even compile or run until you delete this line of error.  In addition, if we omit either the FirstName and LastName assignment statement (to be precise, omit the call of either FirstName or LastNamePropert Get), Student class will raise an error when we try to execute the read-only property FullName.  The trick is every time we use FullName property, the code will check the value stored in our Private member  m_FirstName and m_LastName.  If either of the two property does not contain any value, there is no reason to return the value of FullName. In fact, have you ever met a person with only have a FirstName or LastName?

Comments

  1. 26 Jun 2009 at 13:14

    Wouw, thanks for this great explanation on how to use and make classes. I always coded in VB with function in normal Modules, now i'm going one step further... Trying it out...

  2. 23 Jun 2009 at 10:37

    Great Tutorial !! Helped me a lot

  3. 07 Mar 2009 at 10:54
    This is great tutorial! Thank you sooooo much.
  4. 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.

  5. 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

  6. 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
  7. 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 ?

  8. 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

  9. 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
  10. 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.
  11. 09 Jun 2005 at 17:34

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

  12. 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

  13. 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...

  14. 20 Sep 2003 at 06:32

  15. 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.
  16. 01 Jan 1999 at 00:00

    This thread is for discussions of Introduction to Class Programming.

Leave a comment

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

Dante Salvador

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback