Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
Created June 19, 2011 12:52
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mlhaufe/1034244 to your computer and use it in GitHub Desktop.
Save mlhaufe/1034244 to your computer and use it in GitHub Desktop.
VBScript class constructor parameters
Class Person
Private m_Age
Private m_Name
Public Default Function Init(Name, Age)
m_Name = Name
m_Age = Age
Set Init = Me
End Function
Public Property Get Name
Name = m_Name
End Property
Public Property Let Name(v)
m_Name = v
End Property
Public Property Get Age
Age = m_Age
End Property
Public Property Let Age(v)
m_Age = v
End Property
End Class
Dim TheDude : Set TheDude = (New Person)("John",40)
@gwarah
Copy link

gwarah commented Feb 3, 2017

difference between init() and class_initialize()

option explicit

dim TheDude : Set TheDude = (New foo)("Hendrix",50)
TheDude.show

class foo

    private name
    private age

    public Sub show
        wscript.echo "name: " & name
        wscript.echo "age:  " & cstr(age)
    End Sub

    ' runs after Class_Initialize method
    Public default function init(pNome,pAge)
        wscript.echo "debug: init"
        
        name=pNome
        age=pAge
        
        Set Init = Me
    end function

    runs first but arguments are not allowed
    Private Sub Class_Initialize()
        wscript.echo "debug: Class_Initialize"
        name="James"
        age=40
    End Sub
end class

Output:

F:\snippets\vbscripts>cscript teste_class01.vbs
debug: Class_Initialize
debug: init
name: Hendrix
age:  50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment