15

I found a way to extend classes in VBScript, but are there any ways to pass in parameters or overload the constructor? I am currently using an Init function to initialize the properties, but would like to be able to do this when I create the object.
This is my sample class:

Class Test
    Private strText

    Public Property Get Text
        Text = strText
    End Property

    Public Property Let Text(strIn)
        strText = strIn
    End Property

    Private Sub Class_Initialize()  
        Init
    End Sub  

    Private Sub Class_Terminate()   

    End Sub 

    Private Function Init
        strText = "Start Text"
    End Function    
End Class

And I create it

Set objTest = New Test

But would like to do something like this

Set objTest = New Test(strInitText)

Is this possible, or does the object have to be created and initialized in two setps?

4 Answers 4

24

Just to alter slightly on svinto's method...

Class Test
  Private m_s
  Public Default Function Init(s)
    m_s = s
    Set Init = Me
  End Function
  Public Function Hello()
    Hello = m_s
  End Function
End Class

Dim o : Set o = (New Test)("hello world")

Is how I do it. Sadly no overloading though.

[edit] Though if you really wanted to you could do something like this...

Class Test
    Private m_s
    Private m_i

    Public Default Function Init(parameters)
         Select Case UBound(parameters)
             Case 0
                Set Init = InitOneParam(parameters(0))
             Case 1
                Set Init = InitTwoParam(parameters(0), parameters(1))
             Else Case
                Set Init = Me
         End Select
    End Function

    Private Function InitOneParam(parameter1)
        If TypeName(parameter1) = "String" Then
            m_s = parameter1
        Else
            m_i = parameter1
        End If
        Set InitOneParam = Me
    End Function

    Private Function InitTwoParam(parameter1, parameter2)
        m_s = parameter1
        m_i = parameter2
        Set InitTwoParam = Me
    End Function
End Class

Which gives the constructors...

Test()
Test(string)
Test(integer)
Test(string, integer)

which you can call as:

Dim o : Set o = (New Test)(Array())
Dim o : Set o = (New Test)(Array("Hello World"))
Dim o : Set o = (New Test)(Array(1024))
Dim o : Set o = (New Test)(Array("Hello World", 1024))

Bit of a pain though.

5
  • 2
    It's 2011 and I both googled for this and enjoyed learning it. I like refactoring old VBScript the same way I like getting an old 486 running. I have no idea way. Commented Feb 22, 2011 at 18:04
  • You have to wrap the (New Test) in parentheses? You can't simply do, New Test(Array())? =[
    – crush
    Commented Mar 18, 2013 at 17:54
  • I can't quickly test it but as far as I remember the parentheses are required. There are no class constructors so New ClassName(arguments) is not valid syntax in VBScript. The parentheses make it clear to the parser that you're applying the arguments to the result of New ClassName. Applying arguments to a object calls the function marked as default in the class definition.
    – jammus
    Commented Apr 7, 2013 at 9:01
  • Awesome. There are a few minor errors: missing 'set' in interior functions, end function instead of end public, and end select instead of end case. Commented Apr 10, 2013 at 20:02
  • 3
    Parentheses are not required if explicitly calling Init (e.g. Set o = New Test.Init("hello world"). Unfortunately for the default function trick this does not work, so yes -- parentheses are required in this case.
    – wqw
    Commented Sep 14, 2013 at 16:46
7

You can work around it by having your Init function returning the object itself...

Class Test
  Private m_s
  Public Function Init(s)
    m_s = s
    Set Init = Me
  End Function
  Public Function Hello()
    Hello = m_s
  End Function
End Class

Dim o
Set o = (New Test).Init("hello world")
Echo o.Hello
1
  • Why do I get an error for Set o = (New Test).Init("hello world") but not for Set o = New Test : Set o=o.Init("hello world") if I do all this in a QTP library, and put the Set o= statements into a method? Commented Apr 28, 2016 at 12:00
2

You have to do it in two steps. VB Script doesn't support overloading so you can't modify the default constructor with new parameters. Same goes for Vb6

1

A bit hackish, for sure, but when I need varargs in calls, one of my parameters I pass in as an array, i.e.

Rem printf done poorly
sub printf(fmt, args)
  dim fp, vap:
  dim outs:
  dim fini:
      fini = 0:
      vap = 0:
  while (not fini)
    fp = index(fmt,"%"):
    if (not(isNull(fp))) then
        ' do something with %f, %s
        select case(fp)
        case 'c':
          outs = outs & charparse(args(vap)):
        case 's':
          outs = outs & args(vap):
        ' and so on.  Quite incomplete but you get the idea.
        end select
        vap = vap + 1
    end if
  wend
end sub

printf("%s %d\n",array("Hello World", 42)):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.