Thursday, July 16, 2009

Class Use in VB Scripts

Class Use in VB Scripts:
Here is the basic example of creating a Class. Class ‘Student’ has been created to define the properties of a student. Then created a new instance (objStudent) to use this class definitation. This is a very basic example of Class. Following this class concept, you will be able to write any complex script using class.

Class Student
'In the declaration section of the Student class module
Public StudentID
Public FirstName
Public LastName
Public MajorCode
Public YearLevel
Public BirthDate

End Class

'Declare an object Student
Dim objStudent

'Create an instance of the class
Set objStudent = New Student
On Error Resume Next

'Use the object Student
objStudent.StudentID = "12345" '& CLng(2)
'Err.Raise 6
MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description)
Err.Clear
On Error GoTo 0
objStudent.FirstName = "Cathrina" '& CStr(lngCount)
objStudent.LastName = "Aniversario" '& CStr(lngCount)
objStudent.MajorCode = "C" '& CStr(lngCount)
objStudent.YearLevel = "Freshmen"' & CStr(lngCount)
objStudent.BirthDate = "Oct 10, 1980"' & CDate(lngCount)

MsgBox "Student ID : " & objStudent.StudentID & vbCrLf & _
"Student Name : " & objStudent.FirstName & " " & _
objStudent.LastName & vbCrLf & _
"Major Code : " & objStudent.MajorCode & vbCrLf & _
"Year : " & objStudent.YearLevel & vbCrLf & _
"BirthDate : " & objStudent.BirthDate

Set objStudent = Nothing

No comments:

Post a Comment