VB.Net – 程序结构

在我们学习VB.Net编程语言的基本构建块之前,让我们看看一个最小的VB.Net程序结构,以便我们可以将它作为未来的章节的参考。

VB.Net Hello World示例

一个VB.Net程序主要由以下几部分组成:

  • 命名空间声明  Namespace declaration
  • 一个类或模块  A class or module
  • 一个或多个程序  One or more procedures
  • 变量  Variables
  • 主过程  The Main procedure
  • 语句和表达式  Statements & Expressions
  • 注释  Comments

让我们看一个简单的代码,打印单词“Hello World”:

Imports System
Module Module1
   This program will display Hello World 
   Sub Main()
      Console.WriteLine("Hello World")
      Console.ReadKey()
   End Sub
End Module

当上述代码被编译和执行时,它产生了以下结果:

Hello, World!

让我们来看看上面的程序中的各个部分:

  • 程序Imports System的第一行用于在程序中包括系统命名空间。The first line of the program Imports System is used to include the System namespace in the program.
  • 下一行有一个Module声明,模块Module1。 VB.Net是完全面向对象的,所以每个程序必须包含一个类的模块,该类包含您的程序使用的数据和过程。The next line has a Module declaration, the module Module1. VB.Net is completely object oriented, so every program must contain a module of a class that contains the data and procedures that your program uses.
  • 类或模块通常将包含多个过程。 过程包含可执行代码,或者换句话说,它们定义了类的行为。 过程可以是以下任何一种:
    • 功能 Function
    • 子 Sub
    • 运算符 Operator
    • 获取 Get
    • 组 Set
    • AddHandler
    • RemoveHandler
    • 的RaiseEvent
  • 下一行(这个程序)将被编译器忽略,并且已经在程序中添加了额外的注释。
  • 下一行定义了Main过程,它是所有VB.Net程序的入口点。 Main过程说明了模块或类在执行时将做什么。
  • Main过程使用语句指定其行为

    Console.WriteLine(“Hello World”的)

    WriteLine是在System命名空间中定义的Console类的一个方法。 此语句会导致消息”Hello,World !”在屏幕上显示。

  • 最后一行Console.ReadKey()是用于VS.NET用户的。 这将阻止屏幕从Visual Studio .NET启动时快速运行和关闭。

编译和执行VB.Net程序:

如果您使用Visual Studio.Net IDE,请执行以下步骤:

  • 启动Visual Studio。 Start Visual Studio.
  • 在菜单栏,选择文件,新建,项目。 On the menu bar, choose File, New, Project.
  • 从模板中选择Visual Basic。Choose Visual Basic from templates
  • 选择控制台应用程序。Choose Console Application.
  • 使用浏览按钮指定项目的名称和位置,然后选择确定按钮。 Specify a name and location for your project using the Browse button, and then choose the OK button.
  • 新项目显示在解决方案资源管理器中。 The new project appears in Solution Explorer.
  • 在代码编辑器中编写代码。 Write code in the Code Editor.
  • 单击运行按钮或F5键运行项目。 将出现一个包含行Hello World的命令提示符窗口。 Click the Run button or the F5 key to run the project. A Command Prompt window appears that contains the line Hello World.

您可以使用命令行而不是Visual Studio IDE编译VB.Net程序:

  • 打开文本编辑器,并添加上述代码。 Open a text editor and add the above mentioned code.
  • 将文件另存为helloworld.vb。 Save the file as helloworld.vb
  • 打开命令提示符工具并转到保存文件的目录。 Open the command prompt tool and go to the directory where you saved the file.
  • 类型VBC helloworld.vb,然后按回车编译代码。 Type vbc helloworld.vb and press enter to compile your code.
  • 如果在你的代码中没有错误命令提示符下会带你到下一行,并会产生HelloWorld.exe的可执行文件。 If there are no errors in your code the command prompt will take you to the next line and would generate helloworld.exe executable file.
  • 接下来,输入的HelloWorld来执行你的程序。 Next, type helloworld to execute your program.
  • 您将可以看到“Hello World”字样在屏幕上。 You will be able to see “Hello World” printed on the screen.

作者:terry,如若转载,请注明出处:https://www.web176.com/vbnet/11356.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年3月1日
下一篇 2023年3月1日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注