Categories: VB.Net 教程

VB.Net – 声明

statement 声明是Visual Basic程序中的完整指令。 它可以包含关键字,运算符,变量,字面值,常量和表达式。

语句可以分类为:

1、Declaration statements 声明语句 – 这些语句是您命名变量,常量或过程的语句,也可以指定数据类型。

2、Executable statements 可执行语句 – 这些是启动动作的语句。 这些语句可以调用方法或函数,通过代码块循环或分支,或者将值或表达式赋值给变量或常量。 在最后一种情况下,它被称为Assignment语句。

声明语句

声明语句用于命名和定义过程、 变量、 属性、 数组和常量。当你声明的编程元素时,您还可以定义其数据类型、 访问级别和范围。
您可以声明的编程元素包括变量、 常数、 枚举、 类、 结构、模块、 接口、 程序、 过程参数、 函数返回值、 外部过程的引用、 运算符、 属性、 事件和委托。
以下是在 VB.Net 中的声明语句︰
S.N 声明和说明 示例
1 Dim Statement

Dim语句

Declares and allocates storage space for one or more variables.

声明和分配一个或多个变量的存储空间。

Dim number As IntegerDim quantity As Integer = 100Dim message As String = "Hello!"
2Const Statement
Const语句

Declares and defines one or more constants.
声明和定义一个或多个常量。
Const maximum As Long = 1000Const naturalLogBase As Object = CDec(2.7182818284)
3Enum Statement
枚举语句

Declares an enumeration and defines the values of its members.
声明一个枚举并定义其成员的值。
Enum CoffeeMugSize    Jumbo    ExtraLarge    Large    Medium    SmallEnd Enum 
4Class Statement

类语句

Declares the name of a class and introduces the definition of the variables, properties, events, and procedures that the class comprises.

声明类的名称,并引入该类包含的变量,属性,事件和过程的定义。

Class BoxPublic length As DoublePublic breadth As Double   Public height As DoubleEnd Class
5Structure Statement

结构声明

Declares the name of a structure and introduces the definition of the variables, properties, events, and procedures that the structure comprises.

声明结构的名称,并引入结构所包含的变量,属性,事件和过程的定义。

Structure BoxPublic length As Double           Public breadth As Double   Public height As DoubleEnd Structure
6Module Statement

模块语句

Declares the name of a module and introduces the definition of the variables, properties, events, and procedures that the module comprises.

声明模块的名称,并介绍模块包含的变量,属性,事件和过程的定义。

Public Module myModuleSub Main()Dim user As String = InputBox("What is your name?") MsgBox("User name is" & user)End Sub End Module
7Interface Statement

接口语句
Declares the name of an interface and introduces the definitions of the members that the interface comprises.
声明接口的名称,并介绍接口包含的成员的定义。

Public Interface MyInterface    Sub doSomething()End Interface
8Function Statement

函数语句

Declares the name, parameters, and code that define a Function procedure.

声明定义函数过程的名称,参数和代码。

Function myFunction(ByVal n As Integer) As Double     Return 5.87 * nEnd Function
9Sub Statement

子语句

Declares the name, parameters, and code that define a Sub procedure.

声明定义Sub过程的名称,参数和代码。

Sub mySub(ByVal s As String)    ReturnEnd Sub
10Declare Statement

声明语句

Declares a reference to a procedure implemented in an external file.

声明对在外部文件中实现的过程的引用。

Declare Function getUserNameLib "advapi32.dll" Alias "GetUserNameA" (  ByVal lpBuffer As String,   ByRef nSize As Integer) As Integer
11Operator Statement

运算符声明

Declares the operator symbol, operands, and code that define an operator procedure on a class or structure.

声明的运算符符号、 操作数和在类或结构定义一个运算符过程的代码。

Public Shared Operator +(ByVal x As obj, ByVal y As obj) As obj        Dim r As New obj implemention code for r = x + y        Return r    End Operator
12Property Statement

属性声明

Declares the name of a property, and the property procedures used to store and retrieve the value of the property.

声明属性的名称,以及用于存储和检索属性值的属性过程。

ReadOnly Property quote() As String     Get         Return quoteString    End Get End Property
13Event Statement

事件声明

Declares a user-defined event.

声明用户定义的事件。

Public Event Finished()
14Delegate Statement

委托声明

Used to declare a delegate.

用于声明一个委托。

Delegate Function MathOperator(     ByVal x As Double,     ByVal y As Double ) As Double

可执行语句

可执行语句执行操作。 调用过程,分支到代码中的另一个地方,循环使用几个语句或评估表达式的语句是可执行语句。 赋值语句是可执行语句的一种特殊情况。

以下示例演示了一个决策语句:

Module decisions
   Sub Main()
      local variable definition 
      Dim a As Integer = 10

       check the boolean condition using if statement 
      If (a < 20) Then
          if condition is true then print the following 
         Console.WriteLine("a is less than 20")
      End If
      Console.WriteLine("value of a is : {0}", a)
      Console.ReadLine()
   End Sub
End Module

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

a is less than 20;
value of a is : 10
andy

前端小白,在Web176教程网这个平台跟大家一起学习,加油!

Share
Published by
andy

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

7 天 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

4 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago