Thom Smith
Thom Smith

Reputation: 14086

Trouble with VBScript default properties

I read Eric Lippert's article on default property semantics in VBScript: http://blogs.msdn.com/b/ericlippert/archive/2005/08/30/458051.aspx

It says:

The rule for implementers of IDispatch::Invoke is if all of the following are true:

  • the caller invokes a property
  • the caller passes an argument list
  • the property does not actually take an argument list
  • that property returns an object
  • that object has a default property
  • that default property takes an argument list

then invoke the default property with the argument list. Strange but true.

This seems on its face to be an odd rule, but it's invaluable when you're working with collections. Or at least, it would be, but I can't seem to get it to work.

class Test1
    public property get foo
        set foo = new Test2
    end property
end class

class Test2
    public default property get bar (arg)
        Response.Write arg
    end property
end class

dim t: set t = new Test1
Response.Write TypeName(t.foo) ' => "Test2"
t.foo.bar("Hello, World!") ' => "Hello, World!"
t.foo("Hello, World!") => "Microsoft VBScript runtime error '800a01c2' / Wrong number of arguments or invalid property assignment: 'foo'"

The caller invokes the foo property and passes an argument list. The foo property does not actually take an argument list. The foo property returns an object of type Test2. Test2 has a default property, bar. That default property takes an argument list.

Is this a bug, or am I misunderstanding either the default property semantics or my code?

Upvotes: 9

Views: 1101

Answers (2)

Eric Lippert
Eric Lippert

Reputation: 659956

Well golly. I would have expected that to work. It's probably a bug in the implementation of VBScript property getters, which would make it my fault. Sorry about that.

Since the last person to touch that code was me in 1998, I wouldn't expect a fix to be forthcoming any time soon.

Upvotes: 11

Nilpo
Nilpo

Reputation: 4816

Your problem is in this line:

t.foo("Hello, World!")

Your Test2 class does not have a method named foo. What you mean to do is this:

t.foo.bar = "Hello, World!"

Your code is also incomplete. Unless you intend on your properties being read-only, you should assign setters as well.

Upvotes: -1

Related Questions