0

I have two classes, one initialises with class_initialise, and the other using a default function. The one that uses a default function references the other directly. But I can't see values assigned to its properties. If I change them both to use class_initialise, it works. But I need to (eventually) pass parameters to the initialiser, so default function it is.

class cls1
    Public foo
    Private Sub Class_Initialize
        foo = "foo"
    End Sub
End Class

Class cls2
    Public Bar
    Public Default Function Init()
        Call SetBar()
        Set Init = Me
    End Function
    Private Sub SetBar()
        bar = fooclass.foo & ".bar"
    End Sub
End Class

Dim fooclass: Set fooclass = new cls1
Dim barclass: Set barclass = new cls2

Now

MsgBox fooclass.foo ' Shows "foo"
MsgBox barclass.bar ' is empty, not even ".bar"

I'm sure there's something appallingly simple jumping out of the screen at me, but I can't figure out what it actually is!

1 Answer 1

1

Your

Dim barclass: Set barclass = new cls2

does not call the default method .Init(). You could use

Dim barclass: Set barclass = (new cls2)()

I would prefer

Dim barclass: Set barclass = new cls2.Init()

or

Dim oBar : Set oBar = new cls2.Init()
Sign up to request clarification or add additional context in comments.

1 Comment

dang it! You're of course correct - the init requires brackets in order to execute, whilst class_initiliase is executed if present by the vbscript runtime. Silly me - Cheers :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.