locked
Declaring Variables With or Without Specifying the Type RRS feed

  • Question

  • User2023521679 posted

    I was always taught to explicitly specify the type of variable I was declaring in VB.Net such as Dim X As Integer or Dim S As String. Now, I am noticing lots of examples from Microsoft and others where they do not declare the variable type. This is confusing me. Can someone please clear this up. When is declaring the type of variable mandatory and when is it not? Code similar to what I recently saw in a Microsoft training video is posted below.

     

    Option Strict On
    Option Explicit On
    Module Module1
       Sub Main()
    
          Dim Directories = My.Computer.FileSystem.GetDirectories("C:\")
    
          For Each File In Directories
             Console.WriteLine(File)
          Next
    
          Console.ReadLine()
       End Sub
    End Module

    Thursday, August 15, 2013 5:03 PM

Answers

  • User-760709272 posted

    Your variables *are* getting an explicit type as if you had defined them yourself, however it is the compiler that it working the type out for you.

    This is a bone of contention with some people thinking doing this all the time is good, and some thinking it is bad.  In terms of performance, strong typing etc there is NO difference, the compiler is just working out the type.

    There is a reason this technology was introduced, and that is anonymous types.  As these are defined without having a name that you can know at design time, you leave the type off and the compiler will create the type name for you.

    It can also make some LINQ code a bit easier to read, if you've seen the type that comes back from a group function, for example, it can be easier on the eye to not have that in your code.

    Anonymous types (where this technique is mandatory) are when you do things like

    db.Tables.Where(t => t.customerid == 5).select(new {ID=customerid, FirstName = t.first_name, LastName = t.last_name})

    or when you use the query version of linq syntax.  As I said at the start, though, some developers use this technique with every variable which, IMO, makes the code harder to read as it isn't always obvious what type something is.

    Dim data = SomeFunction

    Is data a string?  Int? Bool? Customer object?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 15, 2013 5:21 PM

All replies

  • User-760709272 posted

    Your variables *are* getting an explicit type as if you had defined them yourself, however it is the compiler that it working the type out for you.

    This is a bone of contention with some people thinking doing this all the time is good, and some thinking it is bad.  In terms of performance, strong typing etc there is NO difference, the compiler is just working out the type.

    There is a reason this technology was introduced, and that is anonymous types.  As these are defined without having a name that you can know at design time, you leave the type off and the compiler will create the type name for you.

    It can also make some LINQ code a bit easier to read, if you've seen the type that comes back from a group function, for example, it can be easier on the eye to not have that in your code.

    Anonymous types (where this technique is mandatory) are when you do things like

    db.Tables.Where(t => t.customerid == 5).select(new {ID=customerid, FirstName = t.first_name, LastName = t.last_name})

    or when you use the query version of linq syntax.  As I said at the start, though, some developers use this technique with every variable which, IMO, makes the code harder to read as it isn't always obvious what type something is.

    Dim data = SomeFunction

    Is data a string?  Int? Bool? Customer object?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 15, 2013 5:21 PM
  • User2023521679 posted

    Thanks, that's a good explanation. I just found the Option Infer Off statment. When I used this, it made me declare the type of all variables.

    Thursday, August 15, 2013 5:25 PM
  • User397347636 posted

    Be aware that if you have Option Infer Off in addition to Option Strict Off, then VB will still allow you to drop the type, but the type will be 'Object'.

    Monday, August 19, 2013 6:40 PM
  • User2023521679 posted

    Yes, that sounds like a pretty bad combination. I don't think I will use Infor Off in general, but will try and explicitly declare the variable type for the most part.

    Tuesday, August 20, 2013 10:03 AM