Search Tools Links Login

Classes In VBScript


This code will allow you to use classes in VBSccript versions 5.0 and higher

Original Author: Alex Karpman

Assumptions

It's suggestes to knows basic OOP, and VB(the syntax in similar). VBScript version 5.0 or higher supports classes. The current version 5.6beta dosen't support events(it supports only methods and properties) or Inheritance. I hope MS will improve those 2 subjects.

Side Effects

NO SIDE EFFECTS

Code

'Class C
Class C
'Method F
Sub F(msg)
MsgBox msg
End Sub
End Class
'Using the class C
Sub Main()
Dim o
Set o = New C

o.f "my message!"
'wow a MsgBox will appear
End Sub
'another example
Class cEmployee
'local variables to hold property values
Private mvarFullName 'local copy
Private mvarAge 'local copy
Private mvarRank 'local copy
Private mvarSalary 'local copy

Public Property Let Salary(ByVal vData)
'used when assigning a value to the property, on the left side of an assignment.
mvarSalary = vData
End Property

Public Property Get Salary()
'used when retrieving value of a property, on the right side of an assignment.
Salary = mvarSalary
End Property

Public Property Let Rank(ByVal vData)
'used when assigning a value to the property, on the left side of an assignment.
mvarRank = vData
End Property

Public Property Get Rank()
'used when retrieving value of a property, on the right side of an assignment.
Rank = mvarRank
End Property


Public Property Let Age(ByVal vData)
'used when assigning a value to the property, on the left side of an assignment.
mvarAge = vData
End Property
Public Property Get Age()
'used when retrieving value of a property, on the right side of an assignment.
Age = mvarAge
End Property

Public Property Let FullName(ByVal vData)
'used when assigning a value to the property, on the left side of an assignment.
mvarFullName = vData
End Property
Public Property Get FullName()
'used when retrieving value of a property, on the right side of an assignment.
FullName = mvarFullName
End Property
End Class

Sub Main()
dim o
set o = new cemployee
o.fullname="Alex Karpman"
o.age=13
o.salary=1000000
o.rank="Big Boss"

msgbox o.fullname
msgbox o.age
msgbox o.salary
msgbox o.rank
msgbox o.fullname & "-" & o.age & "-" & o.salary & "-" & o.rank
end sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 94 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.