Classess in VB script
Class Customer
Private m_CustomerName
Private m_OrderCount
Private Sub Class_Initialize
m_CustomerName = ""
m_OrderCount = 0
End Sub
' CustomerName property.
Public Property Get CustomerName
CustomerName = m_CustomerName
End Property
Public Property Let CustomerName(custname)
m_CustomerName = custname
End Property
' OrderCount property (read only).
Public Property Get OrderCount
OrderCount = m_OrderCount
End Property
' Methods.
Public Sub IncreaseOrders(valuetoincrease)
m_OrderCount = m_OrderCount + valuetoincrease
End Sub
End Class
Dim c
Set c = New Customer
c.CustomerName = "Fabrikam, Inc."
MsgBox (c.CustomerName)
c.IncreaseOrders(5)
c.IncreaseOrders(3)
MsgBox (c.OrderCount)
Property in Class
Class User
' declare private class variable
Private m_userName
Private m_Test
' declare the property
Public Property Get UserName
UserName = m_userName
End Property
Public Property Let UserName (strUserName)
m_userName = strUserName
End Property
Public Property Get Test
Test = m_Test
End Property
Public Property Let Test (sTest)
m_Test = srTest
End Property
' declare and define the method
Sub DisplayUserName
WScript.Echo UserName
WScript.Echo Test
End Sub
End Class
Dim objUser
Set objUser = New User
' set the UserName property
objUser.UserName = "The Guru"
objUser.Test = "Test value"
' call the DisplayUserName method to print the user name
objUser.DisplayUserName
Shared Class
Class Test
Public InstanceValue As String
Public Shared SharedValue As String
Public Shared
Sub ShareMethod()
MsgBox("This is a shared method.")
End Sub
End Class
Sub TestShared()
Dim Shared1 As New Test() ' Create an instance of the class.
Dim Shared2 As New Test() ' Create an instance of the class.
Shared1.SharedValue = "Share Value 1" ' Set the value of a shared field.
Shared2.SharedValue = "Share Value 2" ' Overwrite the first value.
MsgBox("The value of the shared field in the first instance" & _
"is: " & Shared1.SharedValue)
MsgBox("The value of the shared field in the second instance" & _
is: " & Shared2.SharedValue)
' Call a method on the class without creating an instance.
Test.ShareMethod()
End Sub
Object Drive using Class
Dim filesys
set filesys = CreateObject("Scripting.FileSystemObject")
Set drv = filesys.GetDrive("e")
select case drv.DriveType
Case 0: drtype = "Unknown"
Case 1: drtype = "Removable"
Case 2: drtype = "Fixed"
Case 3: drtype = "Network"
Case 4: drtype = "CD-ROM"
Case 5: drtype = "RAM Disk"
End Select
WScript.Echo "The specified drive is a " & drtype & " type disk."
WScript.Echo " "
WScript.Echo "Total size is " & drv.TotalSize & ". "
WScript.Echo "Available space is " & drv.AvailableSpace & ". "
WScript.Echo "Type is " & drv.DriveType & " "
WScript.Echo "Path is " & drv.Path & " "
Dim filesyst, drive
Set filesyst = CreateObject("Scripting.FileSystemObject")
WScript.Echo "Before Condtion"
drive = filesyst.DriveExists("z")
WScript.Echo drive
If filesyst.DriveExists("z") Then
WScript.Echo("The specified drive does exist.")
Else
WScript.Echo("The specified drive does not exist.")
End If
WScript.Echo "After Condtion"
Hi, Nice Blog...keep on posting them...
ReplyDelete-Nivedita Sharma
Thanks Nivedita
ReplyDeleteYou can also put your queries here.. I will try to reply them also.