目录

VB.Net - 声明( Statements)

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

声明可归类为 -

  • Declaration statements - 这些是您为变量,常量或过程命名的语句,也可以指定数据类型。

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

声明声明

声明语句用于命名和定义过程,变量,属性,数组和常量。 声明编程元素时,还可以定义其数据类型,访问级别和范围。

您可以声明的编程元素包括变量,常量,枚举,类,结构,模块,接口,过程,过程参数,函数返回,外部过程引用,运算符,属性,事件和委托。

以下是VB.Net中的声明声明 -

Sr.No 声明和说明
1

Dim Statement

声明并为一个或多个变量分配存储空间。

Dim number As Integer
Dim quantity As Integer = 100
Dim message As String = "Hello!"
2

Const Statement

声明并定义一个或多个常量。

Const maximum As Long = 1000
Const naturalLogBase As Object 
= CDec(2.7182818284)
3

Enum Statement

声明枚举并定义其成员的值。

Enum CoffeeMugSize
   Jumbo
   ExtraLarge
   Large
   Medium
   Small
End Enum 
4

Class Statement

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

Class Box
Public length As Double
Public breadth As Double   
Public height As Double
End Class
5

Structure Statement

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

Structure Box
Public length As Double           
Public breadth As Double   
Public height As Double
End Structure
6

Module Statement

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

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

Interface Statement

声明接口的名称并引入接口所包含的成员的定义。

Public Interface MyInterface
   Sub doSomething()
End Interface 
8

Function Statement

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

Function myFunction
(ByVal n As Integer) As Double 
   Return 5.87 * n
End Function
9

Sub Statement

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

Sub mySub(ByVal s As String)
   Return
End Sub 
10

Declare Statement

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

Declare Function getUserName
Lib "advapi32.dll" 
Alias "GetUserNameA" 
(
   ByVal lpBuffer As String, 
   ByRef nSize As Integer) As Integer 
11

Operator Statement

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

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 
12

Property Statement

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

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

Event Statement

声明用户定义的事件。

Public Event Finished()
14

Delegate Statement

用于声明委托。

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

可执行的声明

可执行语句执行操作。 调用过程,分支到代码中的另一个位置,循环遍历多个语句或计算表达式的语句是可执行语句。 赋值语句是可执行语句的特例。

Example

以下示例演示了决策制定声明 -

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
↑回到顶部↑
WIKI教程 @2018