共用方式為


Visual Basic 中的 Hello World

最後,以下是 Hello World 在 Visual Basic 中的樣子:

清單 1:Visual Basic 中的 Hello World (HelloVB.vb)

' Allow easy reference to the System namespace classes.
Imports System

' This module houses the application's entry point.
Public Module modmain
   ' Main is the application's entry point.
   Sub Main()
     ' Write text to the console.
     Console.WriteLine ("Hello World using Visual Basic!")
   End Sub
End Module

這個程式碼幾乎和 Visual C# 中的程式碼相同。存取核心程式庫的語法是新的 (和 Visual C# 一樣),您指定的是命名空間,而非檔案名稱:

Imports System

除此之外,不需要再多做說明。寫入輸出的命令幾乎和其他語言一樣,特別是 Visual Basic 現在會要求在方法參數前後加上括號。當然,Visual Basic 並不需要用分號來結束陳述式:

Console.WriteLine("Hello World using Visual Basic!")

編譯程式的命令列如下:

vbc.exe /t:exe /debug+ /optionstrict+ /out:.\HelloVB.exe HelloVB.vb

在前一行中,/out 代表輸出檔,/t 代表目標型別。執行包含這個命令列的範例批次檔會得到下列結果:

C:\...\HelloWorld\vb>build

C:\...\HelloWorld\vb> vbc.exe /t:exe /debug+ /optionstrict+ /out:.\HelloVB.exe HelloVB.vb
Microsoft (R) Visual Basic Compiler Version ...
for Microsoft (R) .NET CLR ...
Copyright (C) Microsoft Corp 2001. All rights reserved.

執行產生的可執行檔會得到下列結果:

C:\...\HelloWorld\vb>hellovb
Hello World using Visual Basic!

請參閱

撰寫簡單的 .NET 元件 | 簡單元件的用戶端 | 開發教學課程摘要 | 附錄 A:瀏覽命名空間的工具