VB.Net Tutorial/Language Basics/Option Explicit

Материал из VB Эксперт
Перейти к: навигация, поиск

Cause compiler error when Option Strict On

<source lang="vbnet">Module Module1

 Sub Main()
   Dim AnInt As Integer = 5
   Dim ALong As Long = 7
   ALong = AnInt
   "causes compiler error when Option Strict On
   "AnInt = ALong
   MsgBox(AnInt)
 End Sub

End Module</source>

Option Explicit Off

<source lang="vbnet">Option Explicit Off Module Explicit

  Public Sub Main()
     For ctr As Integer = 0 to 100
        " Do something
        result = cntr
     Next
     Console.WriteLine("The counter reached " & result & ".")
  End Sub

End Module</source>

The counter reached .

Turn Explicit off to use variable without declaration

<source lang="vbnet">Option Explicit Off Module Module1

   Sub Main()
       EmployeeName = "Buddy Jamsa"
       EmployeePhoneNumber = "555-1212"
       EmployeeSalary = 45000.0
       NumberOfEmployees = 1
       Console.WriteLine("Number of employees: " & NumberOfEmployees)
       Console.WriteLine("Employee name: " & EmployeName)
       Console.WriteLine("Employee phone number: " & EmployeePhoneNumber)
       Console.WriteLine("Employee salary: " & EmployeeSalary)
   End Sub

End Module</source>

Number of employees: 1
Employee name:
Employee phone number: 555-1212
Employee salary: 45000