📜  VB.Net-声明

📅  最后修改于: 2020-11-19 08:47:17             🧑  作者: Mango


语句是Visual Basic程序中的完整说明。它可能包含关键字,运算符,变量,字面量值,常量和表达式。

陈述可以归类为-

  • 声明语句-这些是您在其中命名变量,常量或过程的语句,还可以指定数据类型。

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

声明书

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

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

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

Sr.No Statements and Description Example
1

Dim Statement

Declares and allocates storage space for one or more variables.

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

Const Statement

Declares and defines one or more constants.

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

Enum Statement

Declares an enumeration and defines the values of its members.

Enum CoffeeMugSize
   Jumbo
   ExtraLarge
   Large
   Medium
   Small
End Enum 
4

Class Statement

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

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

Structure Statement

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

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

Module Statement

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

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

Declares the name of an interface and introduces the definitions of the members that the interface comprises.

Public Interface MyInterface
   Sub doSomething()
End Interface 
8

Function Statement

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

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

Sub Statement

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

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

Declare Statement

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

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

Operator 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 
12

Property 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
13

Event Statement

Declares a user-defined event.

Public Event Finished()
14

Delegate 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