Library articles and tutorials
Introduction to Class Programming
Method of a Class
In addition to class member (property) data. Your custom class can also include Sub and Function procedures, which are commonly known as methods of the class. A method of a class represent some standard operations on the class itself (properties). As you know, the difference between Sub and Function, is that Sub does not return any value, whereas Function method returns a value. But Visual Basic lets you invoke a function and discard its return value. In our example class, you could easily add a routine that calculate the Student Age.
Function Age() As Integer
' Returns the age in years between 2 dates.
' Doesn't handle negative date ranges i.e. BirthDate > Now()
If Month(Now()) < Month(BirthDate) Or _
(Month(Now()) = Month(BirthDate) And _
Day(Now()) < Day(BirthDate)) Then
Age = Year(Now()) - Year(BirthDate) - 1
Else
Age = Year(Now()) - Year(BirthDate)
End If
End Function
|
As you can see in our example, if you are within the class module, you don't need the dot syntax to refer to the properties of the current instance. In addition, if you refer to a Public name for a property (BirthDate) instead of the corresponding Private member variable (m_BirthDate), Visual Basic executes the Property Get procedure as if the property were referenced from outside the class.
'In your client form MsgBox "Student Age : " & objStudent.Age |
Now let create another function, that checks the validity of YearLevel. We will make this function to be Private meaning that this procedure can only be called from within the module.
'In your Student class module
' Private method of a class, cannot be used outside
Private Function IsValidYearLevel(level As String) As Boolean
Dim varTemp As Variant
Dim found As Boolean
For Each varTemp In Array("Freshmen", "Sophomore", "Junior", "Senior")
If InStr(1, level, varTemp, vbTextCompare) Then
found = True
Exit For
End If
Next
IsValidYearLevel = found
End Function
'In Property Let YearLevel
Property Let YearLevel(ByVal strNewValue As String)
If Not IsValidYearLevel(strNewValue) Then Err.Raise 5
m_YearLevel = strNewValue
End Property
|
In other words, you cannot call this method in your client application. In fact, you cannot see a Private function in IntelSense technology of Visual Basic as shown below.
Related articles
Related discussion
-
ditto
by zapthedingbat (2 replies)
-
Mousewheel
by jonh (3 replies)
-
True multithread VB source code controls
by James Crowley (3 replies)
-
Rely
by Yujvendra Verma (4 replies)
-
True multithread VB source code controls
by James Crowley (3 replies)
[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.
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
This was a very good fundamental article on the use of classes in VB. looking forward to additional more advance content
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 ?
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
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
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.
What a tutorial !! man i like it. this tutorial shows me another side of visual basic wow!!
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
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...
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.