4

Consider the requirement to always declare Option Strict On. We'll always need to declare variables with the As keyword. What would be the type of an anonymous type?

Example : Dim product As ... = New With { Key .Name = "paperclips", .Price = 1.29 }

What will follow the As?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Melursus
  • 10,328
  • 19
  • 69
  • 103

2 Answers2

6

try either setting Option Infer On at the top of the class or a project level

Iain
  • 6,392
  • 2
  • 30
  • 50
  • 1
    +1 To be clear, you are also allowed to have `Option Strict On` and `Option Explicit On` with `Option Infer On`. You should always keep `Option Strict On` and `Option Explicit On`. – MarkJ Aug 11 '10 at 08:34
  • Option Infer On will cause the VB compiler to infer the data type of a variable from the type of its initialization expression. For more information see [Option Infer Statement](http://msdn.microsoft.com/en-us/library/bb384665.aspx) – bstoney Mar 21 '13 at 05:35
1

Add an Option Infer On statement, then you don't use As. If you don't use Option Infer On, product will be of type Object (but you'd have to make Option Strict Off to compile). With type inference on, it will be type (compiler generated).

Richard Anthony Hein
  • 10,550
  • 3
  • 42
  • 62