I initially posted this question on StackOverflow and it's been sitting there unanswered for 2 months now.
As I have stumpled upon this fine forum - apparently with a lot of experienced coders - I shall try again to get me an answer
Yes, this is a theoretical question with little effect on anything. But it's been bothering me. A LOT. Because the VBA compiler doesn't make any sense to me here and I can't find ANYTHING about it on the interwebs. And there are few things more important to me than understanding what I'm actually doing.
I know this code will break at runtime. As I said, this is a theoretical question.
Consider this VBA class module "TestClass":
Code:
Option Explicit
Public TestCol As New Collection
In a standard module, this won't compile:
Code:
Sub Test()
Dim Smth As New TestClass
Smth.TestCol(1) = "Something"
End Sub
Error message given: "Wrong number of arguments or invalid property assignment"
This will compile just fine:
Code:
Sub Test()
Dim Smth As New TestClass
Smth.TestCol.Item(1) = "Something"
End Sub
This will also compile fine:
Code:
Sub Test()
Dim Smth As New Collection
Smth(1) = "Something"
End Sub
It's the same with e. g. dictionaries.
Question: Why will the compiler not allow access to the default member .Item without explicitly stating it?