Results 1 to 3 of 3

Thread: [RESOLVED] Compile-time error 450 on accessing the default member of a collection

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2022
    Posts
    5

    Resolved [RESOLVED] Compile-time error 450 on accessing the default member of a collection

    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?

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Compile-time error 450 on accessing the default member of a collection

    The parentheses in VBx are awfully overloaded. The (1) suffix here can mean 1. a function call with one parameter, 2. an indexed property access, 3. an array indexing, 4. default property + 1. or 2.

    For ID1(1) syntax the compiler uses 3. or 4. but adding more identifier i.e. ID1.ID2 or ID1.ID2.ID3 etc. it starts assuming 1. and 2. cases only.

    You can test TwinBasic compiler and ask in its discussions section on github. I’m sure Wayne will be happy to share implementation details and/or fix non-canonical behavior if found.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2022
    Posts
    5

    Re: Compile-time error 450 on accessing the default member of a collection

    Hey wqweto!

    Thank you for the explanation, now I know more.
    Also thanks for the heads-up on TwinBasic, never heard of it until now. Will keep an eye on it.

    Kind regards
    baelin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width