locked
>Collection Initializers< and 'Option STRICT RRS feed

  • Question

  • What's New in Visual Basic 2010 gives us COLLECTION INITIALIZERS.  I like them   BUT

    Why does the use of >Collection Initializers< generate an error with 'Option STRICT On' ?

    Option Explicit On

     

     

    Private strqwe = {"a", "s"}             is accepted

    Option Strict On             [requires variable declarations
    Private strqwe - {"a", "s"}      to have an 'AS' clause]

    I understand the explanation "needs >AS<' but why did MS Development NOT go the extra step to allow a correctly constucted statement using the >Collection Initializers< technique as "STRICT' {satisfying} programming with an impliied "AS'?

    Friday, August 13, 2010 12:31 PM

Answers

  • If you dont use "AS", then "strqwe" will have the type "Object". Not array or list or any other type of collection. How the compiler could know what type of collection you want

    This imply that the initialization will be done at runtime ... so it will be late binding. And one of the major reason of using "Option Strict On" is to make sure that there is no late binding in the code.

    This is why Option strict will not let you do it without a "As"

    • Proposed as answer by John Anthony Oliver Friday, August 13, 2010 2:01 PM
    • Marked as answer by Chao Kuo Friday, August 20, 2010 6:41 AM
    Friday, August 13, 2010 12:58 PM
  • This is indeed an array initializer and work did take place on this during 2010.

    Array type inference is supported within method bodies so you can omit having to put the parenthesis in to indicate it is an array type.  

    However type inference is supported within method bodies but not outside (ie. fields).   This is not specific to array's but any use of type inference.

    Turning option strict On requires generates errors for items which do not have a type specified or are infered.  So in you case (it is not inferred as its a field, and its not specified with an as clause).   This results in the type being Object as it would have been in previous versions (Backward compatibility) ad hence an error.

    So its a "By Design" albiet not an ideal one.   The issue is not really the array initializer feature but more the type inference isn't supported for fields.

    The same behaviour occurs for non arrays

    Option Strict On
    
    Module Module1
      Dim x1 = {"s", "t"}
      Dim y1 = "s"
    
      Sub Main()
        Dim x2 = {"s", "t"}
        Dim y2 = "s"
      End Sub
    
    End Module
    
    

     

     

    • Marked as answer by Vinelander Friday, August 20, 2010 12:16 PM
    Thursday, August 19, 2010 3:25 PM

All replies

  • You cannot use Option Invert on global level (or create anonymous classes)

    Be aware this is no collection initializer, this is simply as old as Methuselah in VB 

    The new collection initializer in VB in version 2010 is (the with version did already exist in VB 2008) 

    Dim x as new list(of String) from {"a","s"}

     


    Success
    Cor
    Friday, August 13, 2010 12:49 PM
  • Hi,

    I would imagine that needs to be.>>

    Private strqwe() As String = {"a","s"}
    
    'or
    
    Private chars() As Char = {"a"c,"s"c}
    
    'You can also add the New too.>>
    
    Private strqwe() As String = New String() {"a","s"}
    
    'or
    
    Private chars() As Char = New Char() {"a"c,"s"c}
    

     

    OPTION STRICT ON is very fussy, even in earlier versions, if you are going to use it ( recommended ) then you'll get used to this. :-)

     



    Regards,

    John


    Friday, August 13, 2010 12:50 PM
  • If you dont use "AS", then "strqwe" will have the type "Object". Not array or list or any other type of collection. How the compiler could know what type of collection you want

    This imply that the initialization will be done at runtime ... so it will be late binding. And one of the major reason of using "Option Strict On" is to make sure that there is no late binding in the code.

    This is why Option strict will not let you do it without a "As"

    • Proposed as answer by John Anthony Oliver Friday, August 13, 2010 2:01 PM
    • Marked as answer by Chao Kuo Friday, August 20, 2010 6:41 AM
    Friday, August 13, 2010 12:58 PM
  • You cannot use Option Invert on global level (or create anonymous classes)

    Be aware this is no collection initializer, this is simply as old as Methuselah in VB 

    The new collection initializer in VB in version 2010 is (the with version did already exist in VB 2008) 

    Dim x as new list(of String) from {"a","s"}

     


    Success
    Cor

    Hi Cor,

    That makes Methuselah a teenager then or at most in 20's or early 30's, LOL!!

    VB hasn't been around that long!!  :-P

    Not 969 years!! >> http://en.wikipedia.org/wiki/Methuselah



    Regards,

    John


    Friday, August 13, 2010 1:11 PM
  • This is indeed an array initializer and work did take place on this during 2010.

    Array type inference is supported within method bodies so you can omit having to put the parenthesis in to indicate it is an array type.  

    However type inference is supported within method bodies but not outside (ie. fields).   This is not specific to array's but any use of type inference.

    Turning option strict On requires generates errors for items which do not have a type specified or are infered.  So in you case (it is not inferred as its a field, and its not specified with an as clause).   This results in the type being Object as it would have been in previous versions (Backward compatibility) ad hence an error.

    So its a "By Design" albiet not an ideal one.   The issue is not really the array initializer feature but more the type inference isn't supported for fields.

    The same behaviour occurs for non arrays

    Option Strict On
    
    Module Module1
      Dim x1 = {"s", "t"}
      Dim y1 = "s"
    
      Sub Main()
        Dim x2 = {"s", "t"}
        Dim y2 = "s"
      End Sub
    
    End Module
    
    

     

     

    • Marked as answer by Vinelander Friday, August 20, 2010 12:16 PM
    Thursday, August 19, 2010 3:25 PM
  • Don't they say that in good old England, it is a common phrase here for very old. 

    I thought that many phrases at the other side of the North Sea are in a way the same as here.

    And in your link I see this.

    The name Methuselah has become a general synonym for any living creature of great age.

    We use it for everything that is old.



    :-)
    Cor
    Thursday, August 19, 2010 6:09 PM
  • Hi Cor,

    You don't hear of Methuselah much in the U.K.

    Some pensioners may use the name in a sentence like you have shown.

    We in the U.K. may, more often say; "As old as the hills!!"

     



    Regards,

    John


    Thursday, August 19, 2010 10:39 PM
  • Spotty:

    Thanks for clearly defining what I probably should have known, maybe even did know. 

    "albeit not ideal" is perfect explanation and most of us are exactly that.

    Friday, August 20, 2010 12:22 PM