Advertisement
7_2009-2012 Object Oriented Programming (OOP) #228390

Classes In VBScript

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

AI

Podsumowanie AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Kod źródłowy
original-source
'Declare and define a class using the Class statement:
Class cls
	'Private variable to store data:
	Private m_Prop1
	
	'Propety Prop1:
	'Peoperty Let executes when setting the property
	Public Property Let Prop1(ByVal newVal)
		m_Prop1 = newVal
	End Propertya
	
	'Property Get executes when reading it
	Public Property Get Prop1()
		Prop1 = m_Prop1
	End Property
	
	'If the type of the property was class type (and not primitive type) we'd use Property Set instead of Property Get. Property Let souldn't change.
	
	'Property Prop2:
	'Just a public memeber
	'Can't do range-check, or execute code of any kind
	Public Prop2
	
	'Declare and define methods just as you'd write normal functions:'Method F
	Sub foo(msg)
		MsgBox msg
	End Sub
'End the Class statement
End Class

Sub Main()
	'make o a "New cls", like in VB5/6
	Dim o
	Set o = New cls
	
	'Call a method
	o.foo "my message!"
	
	o.Prop1 = "hello"
	o.Prop2 = "world"
	MsgBox o.Prop1 & " " & o.Prop2 & "!"
End Sub
Oryginalne komentarze (3)
Odzyskane z Wayback Machine