Library tutorials & articles
Introduction to Class Programming
- Introduction
- What is a Class
- Why Use Class?
- Fundamental Concepts About Class
- Building Your First Class
- Properties Of a Class
- Property Get/Let
- Read-Only Property
- Write-only property
- Method of a Class
- Choosing Between Methods or Properties
- Summary
Choosing Between Methods or Properties
As you may pointed our earlier. Properties is like a function. So how can we know when we must implement property or a function. To tell you honestly, their is no universal rule concerning this scenario, but usually programmer implement a properties when a routine serves mostly to return a value stored inside the class and can be quickly and easily reevaluated. When the routine servers mostly to evaluate a complex value, they use function. If programmer thinks that in the future the value returned by the routine could be assigned to, they use Property Get procedure and gives them a chance to add a Property Let when its time to implement one.
Let's make an example. Earlier we implement a Property Get procedure for our class member FullName. How can we make our class more useable in a long run by providing a Property Let. This way we can have two way of assign a value to FirstName and LastName property. A possible solution might look like this:
Property Let FullName(ByVal strNewValue As String)
' Return the full name of Student object
Dim aStrName() As String
' Split the argument pass (strNewValue)
aStrName() = Split(strNewValue)
' Raise an error if an FirstName or LastName is empty
If UBound(aStrName) = 0 Then Err.Raise 5
FirstName = aStrName(0)
LastName = aStrName(1)
End Property
|
You can directly assign a value to FirstName and LastName property as shown below:
objStudent.FullName = "Cathrina Anniversario" 'try getting the Student property FirstName and LastName MsgBox objStudent.FirstName 'Invoke Property Get FirstName MsgBox objStudent.LastName 'Invoke Property Get LastName |
As you can see, even if we didn't assign a value to the Student FirstName and LastName property explicitly, our new FullName property does the job. This is other nice thing you can do with class property
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
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...
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...
!--removed tag-->Great Tutorial !! Helped me a lot
!--removed tag-->[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
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
I would appericiate if u can show me the way to learn this programe.
TQ
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...
This thread is for discussions of Introduction to Class Programming.